Skip to content

Instantly share code, notes, and snippets.

@danielsdeleo
Created July 9, 2009 03:14
Show Gist options
  • Save danielsdeleo/143394 to your computer and use it in GitHub Desktop.
Save danielsdeleo/143394 to your computer and use it in GitHub Desktop.
# Updated: This works for every "setter method". I've been using the ivars this whole time (4 YEARS!)
# so I never noticed. But it seems dirty to access ivars you didn't create...
module AccessorDefinition
attr_accessor :foo
end
class Includer
include AccessorDefinition
def foo_is_bar
foo = :bar
end
end
p "## Accessors with Explicit Receivers ##"
obj = Includer.new
p "obj.foo = :bar"
obj.foo = :bar
p "result: #{obj.foo.inspect}"
# => "result: :bar"
p "## Accessors with Implicit Receivers"
obj = Includer.new
p "foo = :bar (in method body)"
obj.foo_is_bar
p "result: #{obj.foo.inspect}"
# => "result: nil"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment