Converting Between Strings and Symbols

Problem

You want to get a string containing the label of a Ruby symbol, or get the Ruby symbol that corresponds to a given string.

Solution

To turn a symbol into a string, use Symbol#to_s, or Symbol#id2name, for which to_s is an alias.

	:a_ 
symbol.to_s # => "a_symbol"
	:AnotherSymbol.id2name # => "AnotherSymbol"
	:"Yet another symbol!".to_s # => "Yet another symbol!"

You usually reference a symbol by just typing its name. If you're given a string in code and need to get the corresponding symbol, you can use String.intern:

	:dodecahedron.object_id # => 4565262
	symbol_name = "dodecahedron"
	symbol_name.intern # => :dodecahedron
	symbol_name.intern.object_id # => 4565262

 

Discussion

A Symbol is about the most basic Ruby object you can create. It's just a name and an internal ID. Symbols are useful becase a given symbol name refers to the same object throughout a Ruby program.

Symbols are often more efficient than strings. Two strings with the same contents are two different objects (one of the strings might be modified later on, and become different), but for any given name there is only one Symbol object. This can save both time and memory.

	"string".object_id # => 1503030
	"string".object_id # => 1500330
	:symbol.object_id # => 4569358
	:symbol.object_id # => 4569358

If you have n references to a name, you can keep all those references with only one symbol, using only one object's worth of memory. With strings, the same code would use n different objects, all containing the same data. It's also faster to compare two symbols than to compare two strings, because Ruby only has to check the object IDs.

	"string1" == "string2" # => false
	:symbol1 == :symbol2 # => false

Finally, to quote Ruby hacker Jim Weirich on when to use a string versus a symbol:

  • If the contents (the sequence of characters) of the object are important, use a string.
  • If the identity of the object is important, use a symbol.

See Also

  • See Recipe 5.1, "Using Symbols as Hash Keys" for one use of symbols
  • Recipe 8.12, "Simulating Keyword Arguments," has another
  • Chapter 10, especially Recipe 10.4, "Getting a Reference to a Method" and Recipe 10.10, "Avoiding Boilerplate Code with Metaprogramming"
  • See http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols for a symbol primer


Strings

Numbers

Date and Time

Arrays

Hashes

Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming

XML and HTML

Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration



Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net