ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Ruby/Tk Primer: Creating a cron GUI Interface with Ruby/Tk
Subject:   Answer to the self.puts dilema
Date:   2004-06-28 18:40:32
From:   christopher_roach

I seem to be having a problem figuring out the posting mechanism here. I posted a reply to the earlier message, but so far I can only view it by clicking on the View link. So rather than trying another reply, I thought I would try making a normal post and hope for the best.

As for the problem your with the call to self.puts, aturley, you are correct. The puts method is a private method of the kernel module included in the main object, and thus, its methods are mixed-in (Ruby's way of implementing multiple inheritance) to the main object. The problem arises in how Ruby handles private methods. In Ruby, private methods cannot be called with an explicit receiver (this includes the self reference). So when you try to prepend the reference to self to the puts call you get a private method error.

I included the following sample class to prove my point.



class MyClass
def sayHello()
self.hello()
end


private
def hello()
puts "Hello, World!!!"
end
end


myClass = MyClass.new()
myClass.sayHello()


Try running it at the command line. You should get an error similar to the error you received when calling self.puts. If you remove private access modifier before the hello declaration, the program should execute properly.

I hope this staightens things out for you. One of the problems I have had with Ruby is fully understanding its access levels.

Thanks for the post.



1 to 2 of 2
1 to 2 of 2