I am an RoR newbie. I have created a small application in ruby which has small functions to execute the code.
e.g.
def abc(xyz)
some code
end
def ghi(xyz)
some code
end
def jkl(output)
some code
end
xyz = abc[ARGV(0)]
output = ghi(xyz)
puts jkl(output)
Now, when I run this code in command prompt using ruby .rb, it executes nicely and returns the desired results. But when I try to create a class and add this whole code to it e.g.
class Foo
def abc(xyz)
some code
end
def ghi(xyz)
some code
end
def jkl(output)
some code
end
xyz = abc[ARGV(0)]
output = ghi(xyz)
puts jkl(output)
end
It generates the error like “undefined method ‘abc’ for Foo:Class (NoMethodError)”
All I want to ask is that how shall I add this code to a class so that it can become more pluggable and get the desired results.
Thanks in advance.
As this is written, these are all instance methods. You need to make them class methods like these two examples, or you could leave them as is and create an instance of the class. Either way, you should probably move the last three statements outside the class definition.
OR….
EDIT: To answer your question in the comments, this is how you would instantiate the class and call using instance methods.
If you don’t have any Ruby learning materials yet, you might want to check out Chris Pine’s tutorial, which includes a section on classes and how they work. As for books, here is a great book for Ruby in general and here is a question regarding books for Rails. I would suggest getting a decent grasp of Ruby before getting too deep into Rails.