This section covers public/private/protected method scope
Ref.: WGR Ch. 5, The default object (self), scope, and visibility
private
keywordself
is the receiving objectclass Midas
def initialize(initial_gold)
@gold = initial_gold
end
def gold
@gold
end
def take_gold_from(other)
@gold += other.gold
end
private :gold
end
>> m1 = Midas.new(10)
>> m2 = Midas.new(20)
>> m1.take_gold_from(m2)
NoMethodError: private method `gold' called
self
is an instance of that class or one of its descendantsclass Midas
protected :gold
end
m1.take_gold_from(m2)
=> 30
private
, protected
, and public
without arguments turn scoping on or offObject
and are available everywhereKernel
are also available everywhererequire
, load
, raise
etc. are Kernel methodsruby -e 'print Kernel.private_instance_methods(false)'