0

我有这样的数组数据:

[#<PriceList id: 463134, distributor_id: 6, brand: "Mann-filter", article_nr: "W712/22", price: 5405.0, quantity: "50", waittime: 1, description: "Фильтр масл OPEL 1.2-3.0L (OC90)", created_at: "2013-01-30 16:35:34", updated_at: "2013-01-30 16:35:34", art_group: "Фильтр масл OPEL 1.2-3.0L (OC90)", oem_number: nil>, #<PriceList id: 517164, distributor_id: 6, brand: "Mann-filter", article_nr: "W712/22", price: 5442.0, quantity: "500", waittime: 3, description: "Фильтр масляный OPEL/GM/DAEWOO", created_at: "2013-01-30 16:42:26", updated_at: "2013-01-30 16:42:26", art_group: "Фильтр масляный OPEL/GM/DAEWOO", oem_number: nil>, #<PriceList id: 463135, distributor_id: 6, brand: "Mann-filter", article_nr: "W712/22(10)", price: 5101.0, quantity: "20", waittime: 1, description: "Фильтр масл.без упак.OPEL/GM (OC90Of)", created_at: "2013-01-30 16:35:34", updated_at: "2013-01-30 16:35:34", art_group: "Фильтр масл.без упак.OPEL/GM (OC90Of)", oem_number: nil>, ... etc

如何更改价格类型?

我试试

@non_original2 = @non_original2.map { |e| e[:price].to_i }

但结果我只看到价格值......我如何更改我的数组,以便所有哈希中的价格字段变为整数值?

4

1 回答 1

2

关于什么

@non_original2.each { |e| e[:price] = e[:price].to_i }

这会更改PriceList列表中的每个项目(并且不会复制列表)。

使用您的方法会产生价格值列表,因为map收集块的结果。结果e[:price].to_i是一个整数(您看到的价格)。

于 2013-07-25T11:57:54.607 回答