Help invoking a method of a class

V

victor.reyes

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
 
B

bbxx789_05ss

Victor said:
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
 
F

funkaster

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,
 
J

JeremyWoertink

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?
 
V

victor.reyes

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
 
B

bbxx789_05ss

Jeremy said:
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]
 
R

robert.dober

Jeremy said:
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
 
V

victor.reyes

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

Jeremy said:
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
 
J

jeremywoertink

7stud said:
Jeremy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top