如何从我的类实例中获取数据?我可以得到这个:无法从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