2011-02-16 87 views
0
class X 
end 
class A 
    def get_it 
     puts @the_variable 
    end 
end 

class B 
    def init_it 
    @the_variable = X.new 
    a = A.new 
    end 
end 

访问呼叫者变量在上面的代码我想要的一类方法来访问X在B实例化的实例红宝石:从声明OBJ实例

回答

2

尝试使用Object#instance_variable_set

class B 
    def init_it 
    @the_variable = X.new 
    a = A.new 
    a.instance_variable_set(:@the_variable, @the_variable) 
end 
end