Hi –
On Sun, 11 Oct 2009, Joshua M. wrote:
David A. Black wrote:
In fact, the
whole private access level depends on it
What do you mean with this exactly?
A private method can only be called without an explicit receiver
(except for =-terminated methods). For example:
class Baker
def bake_cake
make_batter # private method
put_in_oven # private method
puts “Cake is done!”
end
private
def make_batter
puts "Privately making the batter!"
end
def put_in_oven
puts "Privately putting the cake in oven!"
end
end
b = Baker.new
b.bake_cake
In this code, I can call the private methods only when I don’t have
an explicit receiver, including self. If I try to do this:
b.make_batter
I get an error. Also, if I rewrite bake_cake like this:
def bake_cake
self.make_batter
put_in_oven
puts “Cake is done!”
end
I get an error, because I’m calling make_batter with an explicit
receiver.
So Ruby uses the bareword style of method call to enforce privateness.
The exception is the setter methods (like age=), which always require
an explict receiver. In such cases, the receiver must be “self” (i.e.,
exactly the word “self”, not a variable reference to self).
David
–
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com
David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)