On Tue, 11 Nov 2008, Siep K. wrote:
The reason this won’t work is because this variable is not in scope
when the method is run. [To do something similar, i.e., use a variable
inside a “function” when you have declared it outside, you will need
to know about “closures”, but for now just stash that word away for
In which I want it to execute the “text” method, and then, right after
puts “YAY INITIALIZE!”
puts "Yay more text!"
method # or self.method
Which is exactly what I was going to write. Now, you notice that
method just looks like a variable. If you have wotsit = something
(as you can have meaningfully for the accessors ruby creates with
attr_accessor (you’ll find that later))
then ruby will interpret it as a local variable, sometimes. To make
sure it is a method use the self form, but most of the time you don’t
need to. That’s why I was showing you how self varies as you go through
the code. If this is confusing, don’t worry about it, it will make
This sort of thing has not changed since, oh, about the year 2000?
So you can find the free copy of the first edition of Programming Ruby
online, to get these basics into your head. Some things have changed
since, so a newer edition will help till 1.9.1 comes out, when you’ll
be better off with the latest one, same as the rest of us
Posted via http://www.ruby-forum.com/.