Expand All

Symbols

Symbols are, if not unique to Ruby, at least not found in most other languages.

Symbols help Ruby use memory more efficiently. Every time you store a value in an object, Ruby sets aside a little piece of memory to hold it. If enough memory is tied up, we'll see an effect on performance. Perhaps you've seen your laptop run more and more slowly when lots of programs are open, or seen it speed up when you add more RAM.

Symbols let Ruby variables point to the same object in several places instead of allocating a new copy. As a metaphor, think of how a rental car company lets several drivers use the same car instead of buying their own.

Efficient memory use isn't a concern in simple programs, but it's considered good form . At this stage, focus on recognizing and declaring symbols. You'll see symbols regularly as you look at code.

Goals

  • Know a symbol when you see it

  • Learn how to declare a symbol

  • Notice when symbols are used

Step 1

Symbols start with a colon. Let's look at one.

Type this in irb:
    :north
    :north.class
    :north.methods

Step 2

Here are more examples:

Type this in irb:
    :TUESDAY
    :"symbols can have spaces if in quotes but it's a lot to type"
    :'single quotes work too'

Experiment with naming symbols. Do they use the same rules as variables?

Step 3

Symbols are constants, like letters and numbers, so they can't be assigned a value.

Type this in irb:
    :north = :south

Symbols print their names when they're converted to strings.

Type this in irb:
    :Saturday.to_s
    puts "Today is #{:Saturday}"

Symbols are used in a similar way to enums or named constants in other languages - for unchanging ideas that you want to refer to the same way, no matter where you are in a program. Unlike enums and named constants, you never initialize them with a value.

Step 4

How do we know it's the same object? Ruby assigns an object_id number when it creates an object, like a serial number. These are the same object.

Type this in irb:
:friday.object_id
:friday.object_id

Now create two variables with the value 'Friday' and check their object_id.

Ruby allocated a fresh chunk of memory each time, even though the strings says the same thing.

Ruby automatically treats strings, numbers, and some values as symbols.

In irb, check the object ids of a word, your lucky number, true and false, and nil. Check the same value twice to prove it's the same object. ( Tip: to save typing, use the up-arrow key to scroll back through your last irb commands.)

Step 5

Calendars are a good example of when to use symbols. The days and months repeat. In a one year calendar making 52 new Mondays takes some memory, but not too much. What if you wanted to make a ten year calendar, or one with no limits? This is a good place for symbols.

days = [:Monday, :Tuesday, :Wednesday, :Thursday, :Friday, 
        :Saturday, :Sunday]

months = [:January, :February, :March, :April, :May, :June, 
          :July, :August, :September, :October, :November, :December]

today = :Friday
month = :May  

Explicación

Symbols are memory-efficient labels we re-use to keep track of all kinds of things in Ruby. You'll see them again soon in Hashes.

When you see a symbol, think about why the programmer chose to use one. Is the idea important here, or the contents?

Further Reading

The Ruby documentation says: If you’re in doubt whether to use a Symbol or a String, consider what’s more important: the identity of an object (i.e. a Hash key), or the contents.

Next Step: