如何從我的類實例中獲取數據?我可以得到這個:無法從ruby中的類實例獲取對象數據
instance = MyModule::MyClass.new(obj1, obj2)
puts instance
#=> #<MyModule::MyClass:0x0000010120de68>
puts instance.inspect
#=> #<MyModule::MyClass:0x000001019157b0 @obj1=#<MyModule::MyOtherClass:0x00000101915b20 @obj=["stuff", "more stuff", "things"]>, @obj2=#<MyModule::MyThirdClass:0x00000101915a80 @obj=["more things", "even more"]>>
我以爲我可以只是做instance.obj1並從這裏得到@obj數組。這給了我「未定義的方法」。這裏有什麼問題?
但我確實有訪問者! – cheeseandpepper
請發表相關的代碼 – Stefan
我想通了。涉及3個班級。將另外兩個類作爲參數的類有attr_accessors,但其他具有我想要公開的變量的類沒有任何getter/setter。 – cheeseandpepper