Ruby Notes - Symbols
What does it look like?
Starts with a colon, followed by some kind of a name:
walk(:north)
look(:east)
What is it?
You can think of symbols as special strings that are immutable and are only created once. Its value is equivalent to its name and ruby creates it for you.
When should we use it?
They are very useful when you need to use the same string over and over and you would like its value to be immutable, accessible with speed and use very little memory.
Where should we use it?
Because their values don’t change, they are frequently used as keys in hashes.
Any other weird detail?
Because they are used as keys to hashes very frequently so ruby has a shortcut. Instead of:
instrument_section = {
:cello => "string",
:clarinet => "woodwind",
}
you can use:
instrument_section = {
cello: "string",
clarinet: "woodwind"
}