Created
October 29, 2009 19:25
-
-
Save cyberfox/221724 to your computer and use it in GitHub Desktop.
Modules can access instance variables of the included-to class
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
>> module Foo | |
>> def bar | |
>> puts @quux | |
>> end | |
>> end | |
=> nil | |
>> class Baz | |
>> attr_accessor :quux | |
>> def xyzzy | |
>> @quux ||= 1 | |
>> @quux *= 2 | |
>> end | |
>> end | |
=> nil | |
>> Baz.send(:include,Foo) | |
=> Baz | |
>> y2 = Baz.new | |
=> #<Baz:0x565af4> | |
>> y2.xyzzy | |
=> 2 | |
>> y2.bar | |
2 | |
=> nil | |
>> y2.quux = 99 | |
=> 99 | |
>> y2.xyzzy | |
=> 198 | |
>> y2.bar | |
198 | |
=> nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment