On Friday 17 July 2009, Venkat A. wrote:
|> executed until an instance is created and the method is
|> called.
|>
|> Add the following to your third example after the class
|> definition block for Something:
|>
|> Something.new.print_something
|>
|> Gary W.
|
|Thanks for replying Gary and stefano
First of all, I apologize for not having read your message carefully: I
completely missed the fact that the print_something method was inside a
class.
At any rate, most of what I said in my answer is still correct. In the
code
you posted, you didn’t call the Something#print_something but only
defined it.
|This I assume is thrown because the interpreter cannot find the file. I
|ran the irb command from inside the folder containing this file. Doesn’t
|irb see the file?
You need to require the file for irb to see it. Try issuing the
following
command:
require ‘something’
By the way, in the code you posted there’s no mention of something
called
Test. This means that the code you’re running is different from what you
showed us. In this situation, is difficult to for us to help you.
Please,
either show us the complete code or run (and report problems with) the
code
you posted.
|Addressing what steffano said, Well I have called the print_something
|method from inside the class’s constructor. Should I not assume that
|when the file is run the class’s constructor will be called and hence
|the method will be called.
No, you didn’t (at least in the code you posted). As I said above, you
only
defined the method but never actually called it. To do so in a class
constructor, you should have defined an instance method called
initialize and
put a call to print_something there:
class Something
def initialize
print_something
end
end
The initialize method is automatically called when you do Something.new
and,
in turn, it would call print_something. But you didnt’t define an
initialize
method and didn’t call Something.new.
Stefano