What's a Symbol? object_id same for same string in Ruby 2.2+


When running the code in 'What's a Symbol?' I found that, contrary to the text, that the object_id for the two strings "string" was identical. Why?

puts "string".object_id
puts "string".object_id

It ends up that in Ruby 2.2 and greater that Ruby will 'freeze' and de-duplicate strings uses as the object_id in hash literals, the result being the object_id will be the same. This Stack Overflow post has more details.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.