[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: What is the difference between :foo and "foo" ?
From:       Caleb Tennis <caleb () aei-tech ! com>
Date:       2005-12-30 20:57:14
Message-ID: 200512301555.38677.caleb () aei-tech ! com
[Download RAW message or body]

> When I do the following:
>
>   attr_accessor :foo

For me, the colon was the most confusing part.  Ignoring the colon, it 
logically made sense to me that you'd want to do a:

attr_accessor  foo1, foo2, foo3

Then, you realize "hey, I can't do that, because foo1, foo2, and foo3 
represent local variables or methods or some object in the system".

Then I pull out my C/C++ hat and say:

attr_accessor "foo", "foo2", "foo3"

And that makes much more sense, because I have to pass the names somehow to 
attr_acessor - and it's very common to using strings to do this in the C++ 
world.

Then you learn that every time Ruby sees a string it has to create a new 
String object, and there's overhead involved.  Instead, there's a special 
class called Symbol which works kind of like a String, at least for these 
purposes, and once you've ever created one it's always around.  

Then it isn't profound until you see code like this:

1000.times do
  some_object.getProperty("name")
end

and you realize that every time that gets executed a new "name" string has to 
be created, and that's not so nice.  But if you do a:

1000.times do
  some_object.getProperty(:name)
end

Wow, that's much more fantastic.

And eventually you just "get it".


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic