Method: Object#hash
- Defined in:
- object.c
#hash ⇒ Fixnum
Generates a Fixnum hash value for this object. This function must have the property that a.eql?(b) implies a.hash == b.hash.
The hash value is used along with #eql? by the Hash class to determine if two objects reference the same hash key. Any hash value that exceeds the capacity of a Fixnum will be truncated before being used.
The hash value for an object may not be identical across invocations or implementations of Ruby. If you need a stable identifier across Ruby invocations and implementations you will need to generate one with a custom method.
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'object.c', line 162
VALUE
rb_obj_hash(VALUE obj)
{
VALUE oid = rb_obj_id(obj);
#if SIZEOF_LONG == SIZEOF_VOIDP
st_index_t index = NUM2LONG(oid);
#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
st_index_t index = NUM2LL(oid);
#else
# error not supported
#endif
return LONG2FIX(rb_objid_hash(index));
}
|