Skip to content

Commit

Permalink
Tuple: added Tuple#===
Browse files Browse the repository at this point in the history
  • Loading branch information
Ary Borenszweig committed Mar 2, 2016
1 parent 70c5f16 commit 9df8bb9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/std/tuple_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,12 @@ describe "Tuple" do

Tuple.new.last?.should be_nil
end

it "does ===" do
({1, 2} === {1, 2}).should be_true
({1, 2} === {1, 3}).should be_false
({1, 2, 3} === {1, 2}).should be_false
({/o+/, "bar"} === {"fox", "bar"}).should be_true
({1, 2} === nil).should be_false
end
end
33 changes: 33 additions & 0 deletions src/tuple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,39 @@ struct Tuple
true
end

# Returns `true` if case equality holds for the elements in `self` and *other*.
#
# ```
# {1, 2} === {1, 2} # => true
# {1, 2} === {1, 3} # => false
# ```
#
# See `Object#===`
def ===(other : self)
{% for i in 0...@type.size %}
return false unless self[{{i}}] === other[{{i}}]
{% end %}
true
end

# Returns `true` if `self` and *other* have the same size and case equality holds
# for the elements in `self` and *other*.
#
# ```
# {1, 2} === {1, 2, 3} # => false
# {/o+/, "bar"} === {"foo", "bar"} # => true
# ```
#
# See `Object#===`
def ===(other : Tuple)
return false unless size == other.size

size.times do |i|
return false unless self[i] === other[i]
end
true
end

# Implements the comparison operator.
#
# Each object in each tuple is compared (using the <=> operator).
Expand Down

0 comments on commit 9df8bb9

Please sign in to comment.