[Rails] Re: Learning About Symbols
On Friday, July 12, 2013 12:49:11 AM UTC-7, Ruby-Forum.com User wrote:
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.
-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8e38cdaa-b190-4790-b1d7-7504a9838dae%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home