I have the following Ruby
program:
class Access
def retrieve_public
puts "This is me when public..."
end
private
def retrieve_private
puts "This is me when privtae..."
end
protected
def retrieve_protected
puts "This is me when protected..."
end
end
access = Access.new
access.retrieve_protected
When I run it, I get the following:
accessor.rb:23: protected method `retrieve_protected' called for #<Access:0x3925
758> (NoMethodError)
Why is that?
Thanks.
Because you can call protected methods directly only from within instance method of this object, or or another object of this class (or subclass)
class Access
def retrieve_public
puts "This is me when public..."
retrieve_protected
anotherAccess = Access.new
anotherAccess.retrieve_protected
end
end
#testing it
a = Access.new
a.retrieve_public
# Output:
#
# This is me when public...
# This is me when protected...
# This is me when protected...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With