What's the difference between a string and a symbol in Ruby?
问题 What's the difference between a string and a symbol in Ruby and when should I use one over the other? 回答1: The main difference is that multiple symbols representing a single value are identical whereas this is not true with strings. For example: irb(main):007:0> :test.object_id => 83618 irb(main):008:0> :test.object_id => 83618 irb(main):009:0> :test.object_id => 83618 Those are three references to the symbol :test , which are all the same object. irb(main):010:0> "test".object_id =>