From: dblack@... Date: 2006-12-29T22:59:02+09:00 Subject: Re: Pickaxe book questions: Symbols and [] method Hi -- On Fri, 29 Dec 2006, Krekna Mektek wrote: > Hi list, I've got this probably lame questions. > > In the Pickaxe book 2nd ed. they talk about the attr_reader and > attr_writer on pages 28 and 29 of the book. > > There are these attr_reader (eh, methods?) things, which use symbols > instead of variables, why does Ruby use symbols here, what would be > the problem if one uses the variable names instead? The symbol is > pointing to the variable, but the variable is the handle on the data > (variable value) anyway. Like a symlink pointing to a symlink pointing > to the actual file. See Tim's answer. I just wanted to add a nuance: symbols do not point to variables. If you do: x = 1 s = :x there's no special relation between :x and 1, and no special relation between s and x. Internally, Ruby uses :x in a symbol table, indicating that you have a local variable called x. However, *that* use of :x has nothing to do with *your* use of :x. It's very similar to integers. If you assign 10 to a variable, and Ruby uses the number 10 somewhere internally, there's no direct connection. Symbols are a way of representing text. That means they can often be used as method arguments where a method might reasonably be expected to want some text -- such as a method name. But the symbol doesn't "know" that there's a method (or variable) with its name. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)