Created
July 24, 2013 22:14
-
-
Save amiel/6075112 to your computer and use it in GitHub Desktop.
Ruby private weirdness
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 Foo | |
private | |
attr_accessor :foo | |
public | |
def access_foo | |
foo # => nil # as expected | |
self.foo # => NoMethodError: private method... | |
end | |
def set_foo | |
self.foo = 'bar' # => 'bar' # works just fine, presumably because `foo = 'bar' would just set a local variable... | |
end | |
end | |
f = Foo.new | |
f.foo # => NoMethodError: private method `foo` ... # as expected | |
f.foo = 'foo' # => NoMethodError: private method `foo=` # as expected | |
f.set_foo # => 'bar' # works |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment