Tutorial by Examples

The most common way to create a Symbol object is by prefixing the string identifier with a colon: :a_symbol # => :a_symbol :a_symbol.class # => Symbol Here are some alternative ways to define a Symbol, in combination with a String literal: :"a_symbol" "a_symbol&quot...
Given a String: s = "something" there are several ways to convert it to a Symbol: s.to_sym # => :something :"#{s}" # => :something
Given a Symbol: s = :something The simplest way to convert it to a String is by using the Symbol#to_s method: s.to_s # => "something" Another way to do it is by using the Symbol#id2name method which is an alias for the Symbol#to_s method. But it's a method that is unique to the...

Page 1 of 1