Learning About Symbols

Hi Everyone,

I kind of get the idea of symbols. So I need help to understand them more fully.

— config.yml

config:

some_setting: ‘setting_one’

another_setting:

  • ‘element1’

  • ‘element2’

So how do I ingest this:

yaml = YAML.load(‘config.yml’)

Are these true or false?

yaml[‘config’] == yaml[:config]

yaml[‘config’][‘another_setting’] == yaml[:config][:another_setting]

How else do you use symbols?

Thank you.

P

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.

{'a'=>1,:a=>2}

=> {"a"=>1, :a=>2}

{'a'=>1,'a'=>2}

=> {"a"=>2}

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.

{‘a’=>1,:a=>2}

=> {“a”=>1, :a=>2}

{‘a’=>1,‘a’=>2}

=> {“a”=>2}

I know about this behavior, but this doesn’t answer the question I posted.

I tested my question above.

I can create a hash in ruby and explicitly give it symbols for keys and it will work.

But when I retrieve a hash from a yml file, I cannot reference the keys as symbols.

Are these true or false?

yaml[‘config’] == yaml[:config]

yaml[‘config’][‘another_setting’] == yaml[:config][:another_setting]

The answer to both is false.

So that said, where/how else can symbols be used outside of being hash keys? Thanks.

Are these true or false?

yaml[‘config’] == yaml[:config]

yaml[‘config’][‘another_setting’] == yaml[:config][:another_setting]

The answer to both is false.

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.

As it pertains to YAML, String keys and Symbol keys are stored differently:

irb2.0.0> require ‘yaml’

#2.0.0 => true

irb2.0.0> puts({:symbol => “I am a Symbol”, ‘string’ => “I am a String”}.to_yaml)

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.