Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > hash and "=="
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 12th April 19:14
brian buckley
External User
 
Posts: 1
Default hash and "=="



Hello all,

In the Java world when one overrides "equals" in a class it is also
necessary to override "hashCode" so that objects behave properly in
collections. Does the same apply in the Ruby world, ie, it is
necessary to override "hash" when overriding "=3D=3D"?

Are there helper idioms or libraries in Ruby to assist in constructing
"=3D=3D" and "hash" methods (comparable to Java's Jakarta commons
HashCodeBuilder and EqualsBuilder)?

Thanks, =20

--Brian
  Reply With Quote


  sponsored links


2 14th April 04:31
eric hodel
External User
 
Posts: 1
Default hash and "=="



You'll need to define #hash and #eql? as well.


If its a wrapper object, I usually pass through to the wrapper. If
its a composite object something like ^ ing all the components hashes
together works.

Its not hard enough that you need an extra library.
You'll want to define #<=> and include Comparable if you want to sort
your objects sensibly.

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
  Reply With Quote
3 21st April 18:55
brian buckley
External User
 
Posts: 1
Default hash and "=="


What is the difference between "eql?" and "=3D=3D" in Ruby?
  Reply With Quote
4 21st April 22:25
robert klemme
External User
 
Posts: 1
Default hash and "=="


Type conversion for numeric values:

=> false

=> true

But most of the time they behave equivalently.

=> true

=> true

Kind regards

robert
  Reply With Quote
5 23rd April 04:43
eric hodel
External User
 
Posts: 1
Default hash and "=="


#eql? is only used by Hash to determine if two keys (#hash) collide.
Usually it just calls #==.

if object1.hash == object2.hash and object1.eql? object2 then
# both objects occupy the same slot in a Hash
else
# the objects occupy different slots in a Hash
end

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666