J
James
Hi,
I would like to use object instances as hash keys where two objects
containing the same datum both have the same key value. This does not
work by default (as expected given that these are two different
instances):
class Foo
def initialize(datum)
@datum = datum
end
end
# Two hash keys for the same datum
a = {Foo.newbaz) => 1, Foo.newbaz) => 2}
p a
# => {#<Foo:0x2a95e68888 @datum=:baz>=>1, #<Foo:0x2a95e68860
@datum=:baz>=>2}
However, this functionality can be obtained by overriding Object#hash
and Object#eql?:
class Bar
attr_reader :datum
def initialize(datum)
@datum = datum
end
def hash
@datum.hash
end
def eql?(other)
@datum.eql? other.datum
end
end
# One hash key for the same datum
b = {Bar.newbaz) => 1, Bar.newbaz) => 2}
p b
#=> {#<Bar:0x2a95e684f0 @datum=:baz>=>2}
My simple question is if this is safe/normal/OK for my intentions.
Based on perusal of the online Ruby documentation it looks like #eql?
has been overridden in a number of sub-classes for the same purpose.
But I wanted to get feedback from the community - has anyone here
tried this? Are there any known side-effects?
Thanks,
-James
I would like to use object instances as hash keys where two objects
containing the same datum both have the same key value. This does not
work by default (as expected given that these are two different
instances):
class Foo
def initialize(datum)
@datum = datum
end
end
# Two hash keys for the same datum
a = {Foo.newbaz) => 1, Foo.newbaz) => 2}
p a
# => {#<Foo:0x2a95e68888 @datum=:baz>=>1, #<Foo:0x2a95e68860
@datum=:baz>=>2}
However, this functionality can be obtained by overriding Object#hash
and Object#eql?:
class Bar
attr_reader :datum
def initialize(datum)
@datum = datum
end
def hash
@datum.hash
end
def eql?(other)
@datum.eql? other.datum
end
end
# One hash key for the same datum
b = {Bar.newbaz) => 1, Bar.newbaz) => 2}
p b
#=> {#<Bar:0x2a95e684f0 @datum=:baz>=>2}
My simple question is if this is safe/normal/OK for my intentions.
Based on perusal of the online Ruby documentation it looks like #eql?
has been overridden in a number of sub-classes for the same purpose.
But I wanted to get feedback from the community - has anyone here
tried this? Are there any known side-effects?
Thanks,
-James