Carter Davis wrote:
> Yeh, I think I understand. One last thing, though. Say I have this code:
>
> class FirstClass
> def initialize
> puts "YAY INITIALIZE!"
> end
>
> def text
> puts "Yay more text!"
> firstClass.method
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
later.]
> end
>
> def method
> puts "This is what I want it to show."
> end
> end
>
> firstClass = FirstClass.new
> firstClass.text
>
> In which I want it to execute the "text" method, and then, right after
> that, execute the "method" method. Now, if I were to run THIS code, it
> would throw an error, saying that the 'firstClass' variable or method is
> undefined.
>
> How could I make the function execute another method within the same
> object?
Try this:
class FirstClass
def initialize
puts "YAY INITIALIZE!"
end
def text
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 sense
later.
end
def method
puts "This is what I want it to show."
end
end
firstClass = FirstClass.new
firstClass.text
hth,
Siep
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/\.
HTH
Hugh
···
On Tue, 11 Nov 2008, Siep Korteling wrote: