End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Array#hash
array.hash → integer
array.c
View on GitHub ↗
static VALUE
rb_ary_hash(VALUE ary)
{
long i;
st_index_t h;
VALUE n;
h = rb_hash_start(RARRAY_LEN(ary));
h = rb_hash_uint(h, (st_index_t)rb_ary_hash);
for (i=0; i<RARRAY_LEN(ary); i++) {
n = rb_hash(RARRAY_AREF(ary, i));
h = rb_hash_uint(h, NUM2LONG(n));
}
h = rb_hash_end(h);
return ST2FIX(h);
}
Returns the integer hash value for self.
Two arrays with the same content will have the same hash code (and will compare using eql?):
Example
[0, 1, 2].hash == [0, 1, 2].hash # => true [0, 1, 2].hash == [0, 1, 3].hash # => false