2011-02-16 82 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