[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help invoking a method of a class

Victor Reyes

11/2/2007 5:08:00 PM

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

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?
I am sure it is something trivial, but, trivial or not I can't get it.

Thank you

Victor

8 Answers

7stud --

11/2/2007 5:23:00 PM

0

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?
> I am sure it is something trivial, but, trivial or not I can't get it.
>

You need to create an object of the class, and then use the object to
call the method:

class Test
def mag(size)
r1 = Array.new(size)
p r1
end
end

my_obj = Test.new()
my_obj.mag 10


Or, you can create a 'class method', which allows you to call the method
with the class name:

class Dog
def Dog.bark
puts "Woof! woof!"
end
end

Dog.bark

--
Posted via http://www.ruby-....

Rolando Abarca

11/2/2007 5:32:00 PM

0

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



Jeremy Woertink

11/2/2007 6:01:00 PM

0

I think your going to get an undefined method error even after doing
t = Test.new
t.mag 10

you have Array.new(size) {l} <--what is l?

On Nov 2, 10:07 am, "Victor Reyes" <victor.re...@gmail.com> 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?
> I am sure it is something trivial, but, trivial or not I can't get it.
>
> Thank you
>
> Victor


Victor Reyes

11/2/2007 6:17:00 PM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Thank you all for your help.

I agree, I really need to understand OOP.

Thanks again

Victor

On 11/2/07, JeremyWoertink@gmail.com <JeremyWoertink@gmail.com> wrote:
>
> I think your going to get an undefined method error even after doing
> t = Test.new
> t.mag 10
>
> you have Array.new(size) {l} <--what is l?
>
> On Nov 2, 10:07 am, "Victor Reyes" <victor.re...@gmail.com> 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?
> > I am sure it is something trivial, but, trivial or not I can't get it.
> >
> > Thank you
> >
> > Victor
>
>
>

7stud --

11/2/2007 6:18:00 PM

0

Jeremy Woertink wrote:
> I think your going to get an undefined method error even after doing
> t = Test.new
> t.mag 10
>
> you have Array.new(size) {l} <--what is l?


arr = Array.new(3) {1}
p arr

--output:--
[1, 1, 1]
--
Posted via http://www.ruby-....

Robert Dober

11/2/2007 6:39:00 PM

0

On 11/2/07, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> Jeremy Woertink wrote:
> > I think your going to get an undefined method error even after doing
> > t = Test.new
> > t.mag 10
> >
> > you have Array.new(size) {l} <--what is l?
>
>
> arr = Array.new(3) {1}
> p arr
>
> --output:--
> [1, 1, 1]

I am not sure that helps if the posters | (pipe), 1 (one), l
(lowercase L) and I (uppercase i) looks all alike;)
Long live Serif Fonts!!!

HTH
Robert
> --
> Posted via http://www.ruby-....
>
>


--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Victor Reyes

11/2/2007 7:15:00 PM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Oh, yes:
Array.new(size) {l} <--what is l?
That is number 1, the char between {}. It actually populates the array with
ones (1).

Thank you

Victor

On 11/2/07, Robert Dober <robert.dober@gmail.com> wrote:
>
> On 11/2/07, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> > Jeremy Woertink wrote:
> > > I think your going to get an undefined method error even after doing
> > > t = Test.new
> > > t.mag 10
> > >
> > > you have Array.new(size) {l} <--what is l?
> >
> >
> > arr = Array.new(3) {1}
> > p arr
> >
> > --output:--
> > [1, 1, 1]
>
> I am not sure that helps if the posters | (pipe), 1 (one), l
> (lowercase L) and I (uppercase i) looks all alike;)
> Long live Serif Fonts!!!
>
> HTH
> Robert
> > --
> > Posted via http://www.ruby-....
> >
> >
>
>
> --
> what do I think about Ruby?
> http://ruby-smalltalk.blo...
>
>

Jeremy Woertink

11/2/2007 7:43:00 PM

0

7stud -- wrote:
> Jeremy Woertink wrote:
>> I think your going to get an undefined method error even after doing
>> t = Test.new
>> t.mag 10
>>
>> you have Array.new(size) {l} <--what is l?
>
>
> arr = Array.new(3) {1}
> p arr
>
> --output:--
> [1, 1, 1]

OHHHH, that's the number one, I thought it was the letter L :)
oops, my bad.


~Jeremy
--
Posted via http://www.ruby-....