I use Ruby 2.2.2.
irb(main):002:0> str = "abc"
=> "abc"
irb(main):003:0> str2 = "abc"
=> "abc"
irb(main):004:0> str.hash
=> -340360941
irb(main):005:0> str2.hash
=> -340360941
irb(main):006:0> str.object_id
=> 3702564
irb(main):007:0> str2.object_id
=> 24864312
irb(main):009:0> str == str2
=> true
irb(main):010:0> str.eql? str2
=> true
Why str and str2 have same hash, but different object_id? According to doc hash and object_id, no two active objects will share an id, so str and str2 have different object_id, but how to understand their hash are same?
Could anybody tell something about this? My guess is that "abc" only occupy one memory space, and both str and str2 referenced to the same memory space.
If someone could explain it in how memory is allocated, that will be great.
hashandobject_id?