ribes
November 2, 2007, 6:09pm
1
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
ribes
November 2, 2007, 6:23pm
2
Victor R. 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
ribes
November 2, 2007, 6:32pm
3
On Nov 2, 2007, at 2:07 PM, Victor R. wrote:
end # End class
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,
ribes
November 2, 2007, 7:15pm
4
Jeremy W. 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]
ribes
November 2, 2007, 7:20pm
5
Thank you all for your help.
I agree, I really need to understand OOP.
Thanks again
Victor
ribes
November 2, 2007, 7:02pm
6
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?
ribes
November 2, 2007, 7:40pm
7
On 11/2/07, 7stud – [email protected] wrote:
–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
ribes
November 2, 2007, 8:25pm
8
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
ribes
November 2, 2007, 8:42pm
9
7stud – wrote:
Jeremy W. 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
ribes
November 2, 2007, 8:46pm
10
I can do it now if you like (I just wasn’t sure about how long your
meeting would be).