byCoder
byCoder

Reputation: 9184

Ruby change type in array of hashes from float to integer

I have such array data:

[#<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

how can i change price type?

i try

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

but as result i see only price values... How can i change my array, so that price field in all hashes become integer value?

Upvotes: -1

Views: 171

Answers (1)

tessi
tessi

Reputation: 13574

what about

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

This changes every PriceList item in the list (and does not copy the list).

Using your approach results in a list of price values, because map collects the result of the block. The result of e[:price].to_i is an integer (the prices you see).

Upvotes: 2

Related Questions