One reason to use Symbols is that they are immutable. When you're
passing one around as an argument or Hash key, it won't change.
Another is that multiple instances of a Symbol are the same object,
making a smaller memory footprint than Strings.
A string is not a symbol. Some structures will use to_s or to_sym to
allow you to pass either as an argument, but that's not their default
behaviour.
yaml[‘config’] shows me the values from the yml file. yaml[:config] doesn’t.
How else do you use symbols? I have read all the definitions. I understand the explanations, but the rest of where I am confused can now only be answered by example usages.
Symbols are used all the time in Ruby/Rails when sending methods.
For example, look at the following method call:
instance.do_stuff(:foo => :bar, :answer => 3)
What you are actually sending to the do_stuff method is a hash that looks like the following:
{ :foo => :bar, :answer => 3 }
For these purposes, they are basically human-readable identifiers. The advantage they have over strings in this case is that they are re-used, so if you use :foo in your application 100 times, it’s only in memory once. Whereas if you use “foo” 100 times, it’s in memory 100 times.
Thanks guys, that has been very informative. Although, each example has been symbols used in a hash array. Any other uses? Perhaps outside of a “hash”? Thanks.