Friday, July 12, 2013

[Rails] Re: Learning About Symbols

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.

--
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/fba6676d-9f04-4b7e-abc5-e76ce0e90595%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate