2013-07-25 62 views
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 } 

但结果我只看到价格值...我怎样才能改变我的阵列,所以在所有的哈希值这个价格领域成为整数值?

+0

为什么不在你的模型中定义它? –

+0

@MarkThomas它必须是浮动的,只是在一个控制器中必须是int(ceil) –

回答

2

怎么样

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

这个列表中的每变化PriceList项目(并不会复制列表)。

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

+0

不能将符号转换为整数 –

+0

你确定吗?如果你的'@ non_original2 = @ non_original2.map {| e | e [:price] .to_i}'works,'e [:price] .to_i'也可以工作。 – tessi

+0

哦,对不起,我的错误,我的代码是(符号@被更改bu 5!):5!non_original = non_original.flatten! 5!non_original2 = 5!non_original.map {| e | e [:price] .to_i}比你的代码工作,但没有任何事情发生( –