T
Tim Sutherland
I recently discovered the 'set' library after browing www.ruby-doc.org. It's
very useful - I'm using it all the time now. I especially like Set#classify.
One thing that tripped me up:
require 'set'
a = [1, 2].to_set
b = [3, 4].to_set
# Set of Sets
pairs = [a, b].to_set
e = [1, 2].to_set
p(pairs.include?(e)) # -> false
p(a == e) # -> true
p(a.eql?(e)) # -> false
This is with "ruby 1.9.0 (2004-02-28) [i686-linux]".
I've commented on `eql?' before, e.g. Structs used to use reference equality
(before 1.8).
I don't understand why we have eql? and don't just use == in Hashes. If people
occasionally want to use equal? for comparing Hash keys, then why don't we
have
class ReferenceHash < Hash
# Using equal? for comparison.
end
I can't imagine any circumstance where we want to use structural equality
for some types of keys, and equal? for other types. Either you want to use
structural equality for all of them, or equal? for all of them.
Additionally, set.rb defines Set#eql? as
def eql?(o) # :nodoc:
@hash.hash == o.hash
end
@hash is a Hash, and Hash#hash is Kernel#object_id.
This seems like an indirect way to define it (and a problem if Hash#hash was
ever changed to be something else). Why not do
def eql?(o) # :nodoc:
@hash.object_id == o.object_id
end
Or just not define Set#eql? at all and let Kernel#eql? do the same thing.
(Provided that Fixnum#== hasn't been redefined!)
very useful - I'm using it all the time now. I especially like Set#classify.
One thing that tripped me up:
require 'set'
a = [1, 2].to_set
b = [3, 4].to_set
# Set of Sets
pairs = [a, b].to_set
e = [1, 2].to_set
p(pairs.include?(e)) # -> false
p(a == e) # -> true
p(a.eql?(e)) # -> false
This is with "ruby 1.9.0 (2004-02-28) [i686-linux]".
I've commented on `eql?' before, e.g. Structs used to use reference equality
(before 1.8).
I don't understand why we have eql? and don't just use == in Hashes. If people
occasionally want to use equal? for comparing Hash keys, then why don't we
have
class ReferenceHash < Hash
# Using equal? for comparison.
end
I can't imagine any circumstance where we want to use structural equality
for some types of keys, and equal? for other types. Either you want to use
structural equality for all of them, or equal? for all of them.
Additionally, set.rb defines Set#eql? as
def eql?(o) # :nodoc:
@hash.hash == o.hash
end
@hash is a Hash, and Hash#hash is Kernel#object_id.
This seems like an indirect way to define it (and a problem if Hash#hash was
ever changed to be something else). Why not do
def eql?(o) # :nodoc:
@hash.object_id == o.object_id
end
Or just not define Set#eql? at all and let Kernel#eql? do the same thing.
(Provided that Fixnum#== hasn't been redefined!)