Help invoking a method of a class

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 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-forum.com/\.

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,

···

On Nov 2, 2007, at 2:07 PM, Victor Reyes wrote:
--
Rolando Abarca
Phone: +56-9 97851962

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

I can do it now if you like (I just wasn't sure about how long your
meeting would be).

···

-----Original Message-----
From: Victor Reyes [mailto:victor.reyes@gmail.com]
Sent: Friday, November 02, 2007 1:08 PM
To: ruby-talk ML
Subject: Help invoking a method of a class

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

This email and any attached files are confidential and intended solely for the intended recipient(s). If you are not the named recipient you should not read, distribute, copy or alter this email. Any views or opinions expressed in this email are those of the author and do not represent those of the company. Warning: Although precautions have been taken to make sure no viruses are present in this email, the company cannot accept responsibility for any loss or damage that arise from the use of this email or attachments.

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

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-forum.com/\.

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

···

On 11/2/07, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

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

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 :slight_smile:
oops, my bad.

~Jeremy

···

--
Posted via http://www.ruby-forum.com/\.

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-forum.com/\.
>
>

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