From: Christopher Dicely Date: 2010-09-02T00:18:45+09:00 Subject: Re: Array#=== as set equality On Wed, Sep 1, 2010 at 2:03 AM, Intransition 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.