I am confused how to call a function defined in a class inside another function defined in that same class. Here is what I have done:
class Test
def TestFunc(obj)
puts obj
end
def Test.StatFun(obj)
puts obj
TestFunc(obj)
end
end
Test.StatFun([[5,2], [4,3]])
When I run this in cmd.exe, I get the following error:
ruby LawtonTest.rb 5 2 4 3 LawtonTest.rb:10:in
StatFun': undefinedTestFunc’ for Test:Class (NoMet hodError)
method
from LawtonTest.rb:14:in `’
I can’t figure it out. Any help would be greatly appreciated.
You have to call it on the object. I think you need a good reference of oop in Ruby, take a look at http://zetcode.com/lang/rubytutorial/oop/. But anyway, the thing is, methods (which is what you declare with
def) have to be called on an object, not like a global function. So if you want to useTestFunc, try this:The
Test.newpart is used to create an object, on which you can use theTestFuncmethod.