| Article: |
Ruby/Tk Primer: Creating a cron GUI Interface with Ruby/Tk | |
| Subject: | Using Ruby | |
| Date: | 2004-06-28 09:08:26 | |
| From: | r.boylan | |
|
Interesting article after I figured out that you have to use Unix line endings of I did still have trouble prefixing the 'Hello, World!' example with "self." per your explanation. Nothing I tried got past the following error: firstprogram.rb:1: private method `puts' called for #<Object:0xd2cd8> (NameError) I tried both "self.puts" and "self.print" but they both produce the same error (with the obvious method name change). Any ideas or hints? |
||
Showing messages 1 through 2 of 2.
-
Using Ruby
2004-06-28 11:24:35 Christopher Roach |
[View]
-
self.puts
2004-06-28 18:06:54 aturley [View]
Since nobody else has written anything yet, I'll venture a guess. puts is a private method of the "main" object. You can see this by typing self.private_methods. I'm guessing that by calling puts by itself, the interpreter invokes a special private method handler. On the other hand, if you call self.puts, it acts like any old random object had called it, and says, "Oh no you don't! You can't touch my privates." Even though it's trying to access it's own private method. Steamy, eh?
But I'm still learning about Ruby, so I could be wrong.



I'm sorry about the problem you had with the line endings. I nearly always use the Unix line endings so it never occured to me to warn others against using another type.
As for the problem your having with the call to self.puts, well that is occuring because of the way that Ruby handles access control. In the kernel module all of the methods are declared private. 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 a sample class to prove my point.
Try running it at the command line. You should get an error similar to the puts error you received. If you remove the reference to self or if you remove the 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.