From: 7stud -- Date: 2007-11-03T02:23:26+09:00 Subject: Re: Help invoking a method of a class 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/.