Skip to content

Instantly share code, notes, and snippets.

@amiel
Created July 24, 2013 22:14
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 amiel/6075112 to your computer and use it in GitHub Desktop.
Save amiel/6075112 to your computer and use it in GitHub Desktop.
Ruby private weirdness
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