Strings & Symbols in Ruby

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

We use cookies and similar technologies to enhance the quality of services, maintain statistics and adjust marketing content. You will find more information in the Cookies Policy.

By clicking OK you grant consent to processing of your personal data by us and our Trusted Partners with the purpose of maintain statistics and adjustment of the marketing content pursuant to the Privacy Policy. If you wish to not grant that consent and/or limit its extent click Settings.