Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/array_equals.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Determines if the two input arrays have the same count of elements
# and the same integer values in the same exact order
def array_equals(array1, array2)
raise NotImplementedError
if array1 == nil && array2 == nil || array1 == [] && array2 == []
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good boundary checking!

Style nit: this many if-statements all in one line is a bit confusing. (You have to know Ruby's order of operations in order to understand it. 😄 )

  • Good: use parenthesis around the "and" clauses (e.g. if (array1 == nil && array2 == nil) || (array1 == [] && array2 == []))
  • Better: break up each condition (e.g. == nil and == []) into separate "guarding" if-statements

e.g.

# Null/empty checks
if array1 == nil && array2 == nil
  return true
elsif array1 == [] && array2 == []
  return true
elsif array1 == nil || array2 == nil
  return false

# Length check
if array1.length != array2.length
  return false
end

...

You might even be able to do this - but some people would say this
is not explicit enough (or "has too much magic")

if array1 == array2:
  return True
elsif array1 == nil || array2 == nil:
  return False
elsif array1 == [] || array2 == []:
  return False
end

return true
elsif array1 == nil || array2 == nil || array1.length != array2.length
return false
else
i = 0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this block shouldn't be nested inside an else (because the else is redundant here).

array1.each do |x|
Copy link

@ace-n ace-n Apr 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: .each iterates through arrays in order in Ruby, but may not for other data types

Would this break if (for whatever reason) .each did its iteration out of order? If so, how might we fix that?

if x != array2[i]
return false
end
i += 1
end
end
return true
end
7 changes: 4 additions & 3 deletions specs/array_equals_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/array_equals'

require "minitest/autorun"
require "minitest/reporters"
require_relative "../lib/array_equals"

describe "array equals" do
describe "basic tests" do
Expand Down