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

fzwJAFciyxjkYIi

(Anonymous) 2013-05-28 04:59 am (UTC)(link)
That is my impression lennriag Rails I keep thinking, how can people possibly learn this *while* lennriag Ruby? It's hard enough for me to keep track of all of it already knowing Ruby. The effortlessness of Rails stuff is way overhyped. But it is good stuff.

CVsAsciyUkkXQRkaMOS

(Anonymous) 2013-05-28 05:37 am (UTC)(link)
Hi I am so excited I found your web site, I rellay found you by accident, while I was looking on Digg for something else,Anyhow I am here now and would just like to say thank you for a marvelous post and aall round thrilling blog (I also love the theme/design),I don't have time to read through it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the fantastic work.

iNgFQFvpZNVcLVOi

(Anonymous) 2013-05-28 05:43 am (UTC)(link)
Interesting. I wonder how much of the prboelm stems from lack of education. I doubt that it is the whole prboelm given that issues aren't always(often even) fixed after they are bought to the designers attention. However as I read though the article I realized that I have taken 5+ college level computer science courses* and none of the addressed the spefic accessibility issues you mentioned at all. In fact IIRC only the most recent of the classes covered accessiblity at all(we spent about a minute on it). I still only kinda understand why the preview screens are a prboelm and thats just because I read sites like this. Even assuming that it doesn't make them actually care more about accessibility it's way easier for someone to realizes for themselves in advance that a feature is going to be inaccessible to scrap it/fix it with methods they already know than it is for someone to:admit they made a mistake, go back to something they though they were done with improvise a new design, integrate the changes into an existing site, program ect and deal with the backlash from non/differently disabled users who liked the site the original implementation. Trying to outright remove the feature would genrally run into the sunk cost faclicy and even more backlash from users who like the feature not to mention the loss of face for who ever decided the feature was a good idea in the first place. On a semi-related note does anyone have any good recouces on how to desing more accessible interfaces?*Which admitatly don't yet include HCI or the other interface heavy courses that would be the most likely source for this sort of info and could be non-indicative of the state of computer design education overall.

oyLvXUBYwvXgXGe

(Anonymous) 2013-06-08 08:27 pm (UTC)(link)
I'm glad you had lots of takers for your books. They were VERY teitnmpg. But I'm trying hard to divest myself of paper, and I have already replaced those books with bits and sold off my paper copies. So sad to see the shelves empty! So happy to have the books with me everywhere I go!

GOlECkdzeztMhnP

(Anonymous) 2013-06-08 08:55 pm (UTC)(link)
That bottle thorwer.... What a total idiot. The country had pretty much been shaking its collective head and saying, 'What on earth?' We apologise to the rest of the world for our numpties. We'll try and keep them under better control in future.

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