Skip to content

Instantly share code, notes, and snippets.

@scudelletti
Created November 3, 2014 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scudelletti/f38ae41de807f599e30e to your computer and use it in GitHub Desktop.
Save scudelletti/f38ae41de807f599e30e to your computer and use it in GitHub Desktop.
Blog comment - Ruby method visibility
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