【问题标题】:how do i access instance variable of one class into other class define in other file in ruby我如何将一个类的实例变量访问到其他类中定义在 ruby​​ 的其他文件中
【发布时间】:2010-10-08 06:17:50
【问题描述】:

我有一个名为 class1.rb 的文件,这是结构

module Person
  class Amit
    def initialize
      @a=10
    end
  end 
end

另一个名为 class2.rb 的文件

module Person 
  class Sumit < Amit
    def aos
      puts "#{@a}"
    end
  end
end

我无法在Sumit 中访问变量@a 我该怎么做??

【问题讨论】:

  • 将代码贴在你实际使用它的地方并得到错误。

标签: ruby


【解决方案1】:

【讨论】:

  • 但我无法访问此变量@a
  • 我不明白你的问题s = Person::Sumit.news.aos => 10
  • s.instance_variable_get "@a" => 10
  • s.instance_variable_set "@a", 15s.aos=> 15
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
相关资源
最近更新 更多