Created
July 9, 2009 03:14
-
-
Save danielsdeleo/143394 to your computer and use it in GitHub Desktop.
This file contains 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
# 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