Learning About Symbols

47 views
Skip to first unread message

Peter

unread,
Jul 12, 2013, 12:23:31 AM7/12/13
to rubyonra...@googlegroups.com
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

Joel Pearson

unread,
Jul 12, 2013, 3:49:11 AM7/12/13
to rubyonra...@googlegroups.com
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}

--
Posted via http://www.ruby-forum.com/.

Peter

unread,
Jul 12, 2013, 2:51:34 PM7/12/13
to rubyonra...@googlegroups.com
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. 

Peter

unread,
Jul 12, 2013, 2:53:43 PM7/12/13
to rubyonra...@googlegroups.com

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. 

Timster

unread,
Jul 12, 2013, 3:48:20 PM7/12/13
to rubyonra...@googlegroups.com
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.

Rob Biedenharn

unread,
Jul 12, 2013, 3:54:03 PM7/12/13
to rubyonra...@googlegroups.com
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)
---
:symbol: I am a Symbol
string: I am a String
#2.0.0 => nil

Note that there is actually a : (colon) in front of the :symbol and not in front of the string. In both cases there is a : after the key and before the value. (You might also note that the default value is a string and quoting is often implied.)

Here are more types so you can see how they get formatted as YAML:

irb2.0.0> puts({:symbol => "I am a Symbol", 'string' => "I am a String", 42 => "I am the Answer", "41" => "I am not a number", "NumberFive" => 5, "String5" => "5", ' quote me ' => "  I need quotes to keep my leading/trailing spaces"}.to_yaml)
---
:symbol: I am a Symbol
string: I am a String
42: I am the Answer
'41': I am not a number
NumberFive: 5
String5: '5'
' quote me ': '  I need quotes to keep my leading/trailing spaces'
#2.0.0 => nil


-Rob

Peter

unread,
Jul 12, 2013, 9:01:16 PM7/12/13
to rubyonra...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages