It looks like Array#== and Array#=== are the same. Is that correct?
That being so, would there be any serious consequences if Array#===
were redefined to mean “set equality”, e.g.
[:a,:b] === [:b,:a] #=> true
I can think of a few times that would have been nice to have,
especially when testing and hashes come into play, the results often
do not have a fixed order. e.g.
On Wed, Sep 1, 2010 at 11:03 AM, Intransition [email protected]
wrote:
It looks like Array#== and Array#=== are the same. Is that correct?
That being so, would there be any serious consequences if Array#===
were redefined to mean “set equality”, e.g.
I’d prefer
assert r == [[:a,1], [:b,2]]
yup but in your case could you not do something like
assert_equal h, Hash[*r.flatten]
I say that because I’d prefer #=== to have the semantics of include?,
so that I can do
case some_value
when some_array
when other_array
end
On Wed, Sep 1, 2010 at 2:03 AM, Intransition [email protected]
wrote:
It looks like Array#== and Array#=== are the same. Is that correct?
That being so, would there be any serious consequences if Array#===
were redefined to mean “set equality”, e.g.
 [:a,:b] === [:b,:a]   #=> true
Well, it breaks existing code that relies on the existing behavior in
case statements (don’t know how common that is), and if it was going
to be redefined, I think it would be more natural to have it reflect
inclusion (like Range#===) so that, e.g.,
[:a,:b] === :a #=> true
OTOH, if you need something that is initialized by a value of a
particular type but provides special match semantics, its pretty
trivial to do; given the number of different things people might want
#=== to do on a particular class, and existing code that depends on
the existing behavior, its usually probably best not to change #===
for existing (especially core) classes, but just to define matcher
classes as needed. E.g.,
class OrderInsensitiveArrayMatcher
def initialize(array) @array = array.dup
end
def ===(other)
identity = ->(x) {x} @array.group_by(&identity) == other.group_by(&identity)
rescue
nil
end
end
require ‘set’
class SetMatcher
def initialize(enum) @set = Set.new(enum)
end
def ===(other) @set == other.to_set
rescue
nil
end
end
etc.
If there are common use cases, libraries of useful matchers could be
built so that everyone isn’t reinventing the wheel, but without
breaking existing code.