【发布时间】:2013-10-05 14:45:11
【问题描述】:
我有一个名为Product 的类,其中包含name、price 和count。
在另一个名为Shop 的类(包括Product 类)中,我使用一个空数组进行初始化。然后使用方法push 将product 添加到该数组中。
问题发生在Shop类中的方法to_s:
def to_s
string = "Products in the shop:\n"
@list.each do |list|
#### how can i get access to the attributes of product like @list.name or something like that ? I have tried loads of different methods but i cant get access to my attributes. (the array doesnt recognize the product class)
puts @list.name
end
如果我在不使用Array 的情况下创建Product,我可以访问属性 - 我猜问题出在Array...
【问题讨论】:
-
首先向我们展示您创建的课程,然后向我们提出问题,指出您遇到的困惑..
-
代码中的
end是each块的结尾还是def to_s块的结尾? -
无论是谁编辑了它,您可能已经更改了 OP 的问题。请注意我上面关于不匹配的
ends 的评论。 =)