简体   繁体   English

关于ruby中的数组的问题

[英]Questions about arrays in ruby

I have a class called Product , which contains a name , price and count . 我有一个名为Product的类,其中包含namepricecount

In another class called Shop (including the class Product ), I initialize with an empty array. 在另一个名为Shop的类中(包括Product类),我使用一个空数组进行初始化。 Then a add product to this array using the method push . 然后使用push方法将product添加到此数组。

The problem happens to the method to_s of in the Shop class : 问题发生在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

If I create a Product without using Array , I can access to the attributes - I guess the problem comes because of the Array ... 如果我在不使用Array情况下创建Product ,则可以访问属性-我猜问题是由于Array ...

So first of all, your blocks don't match up. 因此,首先,您的块不匹配。

You want: 你要:

def to_s
  string = "Products in the shop:\n"
  @list.each do |item|
    string << item.name + "\n"     # not @list.name! @list is an Array. item is an element of your list.
  end
  string # you could also say: return string
end

You don't want to use puts because that'll send it to the console -- when you're trying to store it in a String called string . 您不想使用认puts因为它会将它传送到控制台-当您试图将它存储在一个叫做string的String中。 You want to make sure you return that, too. 您也要确保返回该值。

You don't have to call your block parameter item . 您不必调用块参数item You could call it list , but that would be confusing, because you have another variable called @list , and the block parameter is not a list at all -- just an item from the array. 可以将其称为list ,但这会造成混淆,因为您还有另一个名为@list变量,而block参数根本不是列表-只是数组中的一项。

Note that this is not the most efficient way to accomplish this. 请注意,这不是完成此操作的最有效方法。 The preferred way is like so: 首选方式如下:

def to_s
  products_list = @list.map do |item|
    item.name
  end
  "Products in the shop:\n" + products_list.join("\n")
end

Each element in @list is yielded to the each block as list . @list每个元素each作为list产生到eachlist

Assuming @list is an Enumerable with Product objects, you should be able to do list.name to get at the name attribute inside the block. 假设@list是一个带有Product对象的Enumerable,则您应该能够执行list.name来获取块内的name属性。

I'd prefer map and join over each and << (just a preference), and braces ( {} ) over do end because I like to imply a return value is important by using braces, and a do end block is there for side effects, eg 我更喜欢mapjoin不是each<< (只是一个首选项),而大括号( {} )在do end之上do end因为我想暗示通过使用大括号,返回值很重要,并且do end块在旁边效果,例如

def to_s
  "Products in the shop:\n" << @list.map{|item| item.name}.join("\n")
end

I also wouldn't use puts because to_s should return a string, not output a string. 我也不会使用puts因为to_s应该返回一个字符串,而不是输出一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM