Skip to content

Instantly share code, notes, and snippets.

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 cyberfox/221724 to your computer and use it in GitHub Desktop.
Save cyberfox/221724 to your computer and use it in GitHub Desktop.
Modules can access instance variables of the included-to class
>> 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