| Sign In/My Account | View Cart |
| 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.
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.
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 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.
But I'm still learning about Ruby, so I could be wrong.