# Pickaxe book questions: Symbols and \[\] method

**URL:** <https://www.ruby-forum.com/t/pickaxe-book-questions-symbols-and-method/80624>\
**Category:** Ruby\
**Created:** [December 29, 2006, 12:53pm UTC](https://www.ruby-forum.com/t/pickaxe-book-questions-symbols-and-method/80624 "2006-12-29T12:53:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [December 29, 2006, 12:53pm UTC](https://www.ruby-forum.com/t/pickaxe-book-questions-symbols-and-method/80624/1 "2006-12-29T12:53:40Z")

</div>

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.

And on page 44 (Containers, Implementing a SongList container)

class SongList  
def   
@songs[index]  
end  
end

Why is there this [] method in the class SongList, when one is able to  
use the [] method on the songs array anyway? Is this because of the  
church of good design, that one must access or change the @songs array  
via accessor methods, instead of changing the array directly?

Krekna

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [December 29, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/pickaxe-book-questions-symbols-and-method/80624/2 "2006-12-29T13:49:47Z")

</div>

Krekna M. wrote:

> to the actual file.

attr\_reader and its friends are methods in the Module class. The symbols  
are arguments to the method that specify the names of the reader methods  
to be defined. You could certainly use a variable in the argument list,  
just like you can for any other method call, but in that case  
attr\_reader would use the value of the variable as the method name, not  
the variable name itself. That is,

foo = “xxx”  
attr\_reader foo

defines the “xxx” method, not the “foo” method. Of course this may be  
exactly what you want to happen. It’s a common technique in Ruby  
metaprogramming.

> church of good design, that one must access or change the @songs array  
> via accessor methods, instead of changing the array directly?

You can’t access @songs outside of the method definition itself, so if  
you want to provide a way of indexing it to the users of the class you  
must provide a method such as this one.

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [December 29, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/pickaxe-book-questions-symbols-and-method/80624/3 "2006-12-29T15:00:07Z")

</div>

Hi –

On Fri, 29 Dec 2006, Krekna M. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [December 29, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/pickaxe-book-questions-symbols-and-method/80624/4 "2006-12-29T16:42:31Z")

</div>

Krekna M. wrote:

> class SongList  
> def   
> @songs[index]  
> end  
> end
> 
> Why is there this [] method in the class SongList, when one is able to  
> use the [] method on the songs array anyway?

This (alone) allows the consumer of the class to write code like:  
p my\_song\_list[3]

If you wanted to do what you suggest, you’d need to add code like:  
class SongList  
attr\_reader ‘songs’ # You can use strings too, if you like  
end

p my\_song\_list.songs[3]

The upside of that is that you immediately get access to all the  
methods of the underlying array, such as…  
my\_song\_list.songs.each{ |song| … }  
…which the former code (alone) doesn’t make possible.

The downside, though, is that you have this class that’s supposed to be  
a list…and then you need to use a property of it to get at the list?  
Where does the madness end? Can you imagine if your class design  
required users to write code like:  
p my\_song\_list.songs.members.items.all[3]

I think that’s the rationale behind the method you see. It shows how  
you can provide a good-looking interface to your own classes.

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [December 31, 2006, 12:40am UTC](https://www.ruby-forum.com/t/pickaxe-book-questions-symbols-and-method/80624/5 "2006-12-31T00:40:44Z")

</div>

> 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.

This is good to remember. Symbols aren’t references or strings.

The reason you give attr\_reader a symbol is not because the symbol  
automatically redirects to the attribute, or that it “knows” anything  
about what it’s pointing to. You give attr\_reader a symbol because the  
attr\_reader code needs some representation of the attribute’s name.  
Since this is text internal to the program itself, you use a symbol.
