Strings & Symbols

Strings

String class is a built-in Ruby object instances of which (strings) are used to denote words.

There can be multiple strings with the same value. Strings with the same value are different objects although they evaluate to true when compared.

a = "ruby"
b = "ruby"
a == b # true
a.equal?(b) # false
a.object_id # 70275395962400
b.object_id # 70275396044460

Symbols

Symbol class is a built-in Ruby object instances of which (symbols) are used as memory efficient names during Ruby runtime. For example, when a method is defined it returns a symbol. Furthermore, a symbol - just like a string - can be used as a hash key name.

Symbols are denoted by words but adversely to strings they are not mutable.

There can be only one symbol with a given value in a Ruby program.

a = :ruby
b = :ruby
a.equal?(b) # true
a == b # true
a.object_id # 676828
b.object_id # 676828