Created
December 17, 2009 09:19
-
-
Save jmeridth/258643 to your computer and use it in GitHub Desktop.
access ruby object from modules via extend
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 MA | |
class B | |
attr_accessor :var_1, :var_2 | |
def initialize | |
@var_1 = "initialized" | |
@var_2 = "initialized" | |
end | |
end | |
end | |
class A | |
def test | |
self.extend MA | |
b = MA::B.new | |
puts "B's instance variables: #{b.instance_variables}" | |
puts "B: #{b.inspect}" | |
b.var_1 = "re_initialized" | |
b.var_2 = "re_initialized" | |
puts "B: #{b.inspect}" | |
end | |
end | |
a = A.new | |
a.test | |
=> B's instance variables: @var_2@var_1 | |
=> B: #<MA::B:0x26a34 @var_2="initialized", @var_1="initialized"> | |
=> B: #<MA::B:0x26a34 @var_2="re_initialized", @var_1="re_initialized"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment