elz: (farewell sunnydale)
elz ([personal profile] elz) wrote in [community profile] rubyonrails2009-04-18 11:32 am

Symbols!

Symbols in Ruby are the things that start with colons: :name, :muffins, :buffy_the_vampire_slayer. They can be a little tough to wrap your brain around (they confused the heck out of me for a while), so I thought I'd do a rundown.



Symbols aren't variables

You can't give them values; :name = "Buffy" will just give you an error.

Symbols aren't strings

"buffy" is not the same as :buffy. You can't do the same things to a symbol that you can do to a string: you can't add them together or subtract things from them, you can't change their case, you can't find out how long they are or generally mess around with them. You can turn a symbol into a string (:buffy.to_s) and you can turn a string into a symbol ("buffy".to_sym), but a symbol on its own isn't very flexible.

Tip: :buffy.to_s gives you "buffy" and "buffy".to_sym gives you :buffy, but "Buffy the Vampire Slayer".to_sym gives you :"Buffy the Vampire Slayer" because of the spaces. It looks a little weird, but it works the same way any other symbol does.

What symbols are used for

The most common place you'll find a symbol is as the key in a hash:

{:name => "Buffy", :address => "Sunnydale, CA", :job => "Vampire Slayer"}

:name doesn't need to be a variable; it doesn't contain any information. And it doesn't need to be a string; you're not doing anything with it or to it except using it to find a value. A good rule of thumb might be that anywhere you need an identifier that doesn't need to hold any other information or be changed or analyzed or displayed, that's a job for a symbol.

Why bother using symbols when you could just use strings

Symbols use less memory, and so they make your program more efficient. How do they do that? Since symbols are immutable and inflexible and generally no fun at parties, Ruby says, well, one :name is the same as any other :name, so I'll just store one copy of it in memory and give you that same copy anywhere you use it. It doesn't matter if one hash has {:name => "Buffy"} and another has {:name => "Faith"}, because :name isn't a variable, and it isn't holding that information for you. It's just a label.

Strings, on the other hand, all get their own spaces in memory. "muffins" and "muffins" live in two different places in memory even though they look exactly the same. That way something like "muffins".upcase! only affects one set of muffins, and not all the muffins you've ever created.

ext_81501: (Default)

[identity profile] sidra.insanejournal.com 2009-04-18 07:35 pm (UTC)(link)
Very nice writeup.

You might also want to add that since strings and symbols are not the same myhash[:key] != myhash["key"] for regular hashes.

That means if you copy your paramaters hash from the error page during testing with something like
> params={"key" => "value"}
when you paste your code from your controller which says
> if params[:key] == "value"
it will fail....

Rails has a special HashWithIndifferentAccess class just for the parameters :)
So, if you do
> params=HashWithIndifferentAccess.new
first, followed by
> params=params.merge({"key" => "value"})

then params[:key] == "value" will be true

nBVzwiDWyWRRkK

(Anonymous) 2013-05-28 06:04 am (UTC)(link)
Ok, well this is showing my age, but what the hell I have never been one to be afaird or ashamed of my age LOL .. But I would have to say that the first shows I remember that had anything to do with paranormal. One was Bewitched, Then of course I can remember watching Chiller on the weekends, that would either have a Dracula movie with Bella L. or a werewolf movie with Boris, (ok I can not think of his last name LOL) But that was along time ago. LOL .. And Lynn is is hard to believe that you are old enough to have grown up on those shows you named. LOL . As far as Buffy, well never got into her show, but I did love Angel when it came out. And my favorite was Charmed and I still love watching it.Thanks for the throwback memories Lynn!!!Teresa