Rolando Abarca
11/2/2007 5:32:00 PM
On Nov 2, 2007, at 2:07 PM, Victor Reyes wrote:
> I started playing with classes and to that effect, I put this simple
> do-nothing class just to make sure I can invoke a method within a
> class.
>
> class Test
> def mag(size)
> maxLoop = size * size
> r1 = Array.new(size) {1}
> p r1
> end # End method mag
> end # End class
>
> mag.Test 9
>
>
> I am getting this error:
>
> C:\$user\ruby\Programs\DejaVou>ruby Test.rb
> Test.rb:9: undefined local variable or method `mag' for main:Object
> (NameError)
>
> Any ideas anyone?
first, you're defining a instance method
second, the convention to call a method in ruby is: receiver.method,
so in this case, you should call the method like this:
Test.mag 9
although, this won't work either, because the method mag is an
instance method, so you should first create an instance, with 'new':
Test.new.mag 9
Anyway, at this point, I would suggest you to read something in
Object Oriented Programming... I think a lot of good pointers have
been suggested here in the list before, so make sure you check the
archives.
> I am sure it is something trivial, but, trivial or not I can't get it.
>
> Thank you
>
> Victor
regards,
--
Rolando Abarca
Phone: +56-9 97851962