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.
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
[: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.
h = {:a=>1, :b=>2}
r = h.map{ |k,v| [k,v] }
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
Cheers
Robert
···
On Wed, Sep 1, 2010 at 11:03 AM, Intransition <transfire@gmail.com> wrote:
won't work b/c it might also return
[[:b,2], [:a,1]]
--
The best way to predict the future is to invent it.
-- Alan Kay
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.
···
On Wed, Sep 1, 2010 at 2:03 AM, Intransition <transfire@gmail.com> 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.
Well, you're talking about set semantics. Why not :
r.to_set == [[:b, 2], [:a, 1]].to_set
=> true
?
You could define things like assert_as_set which maps the arguments with
to_set for you, for instance.
(Note that I'm not a regular user of the unit testing frameworks.)
Fred
···
Le 01 septembre à 11:03, Intransition a écrit :
--
Two things are infinite: the universe and human stupidity; and I'm not
sure about the the universe. (Albert Einstein)