On Dec 28, 2005, at 1:47 PM, Steve Litt wrote: > On Wednesday 28 December 2005 02:32 pm, Alex Knaub wrote: >> 2005/12/28, Surgeon <biyokuantum / gmail.com>: >>> Hi, >>> >>> I am a Ruby newbie. I wish I didn't post such a simple question here >>> but I had to. >>> What is the difference between :foo (a keyword) and "foo"(a string). >>> Can they be used interchangeably? Are they fundamentally same and is >>> the only difference performance? >> >> http://onestepback.org/index.cgi/Tech/Ruby/ >> SymbolsAreNotImmutableStrings.re >> d > > The preceding URL tells me unequivically that symbols aren't > strings, but > really doesn't tell me too much about what they are, other than what, > names??? As one of the people guilty of saying what that article says we shouldn't, I better try to get back in Jim's good graces by answering this one... ;) > I still don't understand why it's > > attr_reader :fname, :lname > > instead of > > attr_reader @fname, @lname > > How does attr_reader know that :fname corresponds to @fname. Seems > like magic > to me. Attributes of a class logically correspond to instance variables in many cases, don't you think? Ruby's just making that assumption for you. When I see: some_call @my_variable I expect what is held inside of @my_variable to get passed to some_call(), not the variable name itself. What you describe would be the opposite and that would surely surprise a lot of people. Furthermore, Symbols are commonly used to refer to method names (as Ruby uses them for this internally). That's really what we are doing here, creating new methods by name, so it's a good fit. Hope that makes some sense. James Edward Gray II