Created
November 3, 2014 23:57
-
-
Save scudelletti/f38ae41de807f599e30e to your computer and use it in GitHub Desktop.
Blog comment - Ruby method visibility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample | |
attr_accessor :protected_attribute, :private_attribute | |
protected :protected_attribute= | |
private :private_attribute= | |
def initialize | |
self.protected_attribute = 'some protected value' | |
self.private_attribute = 'some private value' | |
end | |
end | |
sample = Sample.new | |
sample.protected_attribute | |
#=> some protected value | |
sample.private_attribute | |
#=> some private value | |
sample.protected_attribute = 'foo' | |
#=> NoMethodError: protected method `protected_attribute=' called for #<Sample:0x007fd2bb090c78> | |
sample.private_attribute = 'bar' | |
#=> NoMethodError: private method `private_attribute=' called for #<Sample:0x007fd2bb090c78> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment