Ruby Type Error: no implicit conversion of Symbol into Integer

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Ruby Type Error: no implicit conversion of Symbol into Integer


rideshare_data =
DR0001: [

DATE: "02_03_2016",
COST: 10,
RIDER_ID: "RD0003",
RATING: 3
,

DATE: "02_03_2016",
COST: 30,
RIDER_ID: "RD0015",
RATING: 4
,

DATE: "02_05_2016",
COST: 45,
RIDER_ID: "RD0003",
RATING: 2

],
DR0002: [

DATE: "02_03_2016",
COST: 25,
RIDER_ID: "RD0073",
RATING: 5
,

DATE: "02_04_2016",
COST: 15,
RIDER_ID: "RD0013",
RATING: 1
,

DATE: "02_5_2016",
COST: 35,
RIDER_ID: "RD0066",
RATING: 3

]


rideshare_data.each do |driver_id, driver_data|
money_earned = 0
driver_data.each do |rides|
rides.each do |ride, ride_data|
money_earned = [ride_data][:COST].reduce(:+)
puts "#driver_id earned #money_earned total"
end
end
end



So I have a dataset of arrays and hashes, and I am trying to find the total money earned for each driver by summing the :COST keys. No matter what I try in the last block, I get the error no implicit conversion of Symbol/Hash/String into Integer. Help would be appreciated!




1 Answer
1



Since you're using on an array, Ruby expects you to pass an integer, as arrays can only be accessed by their index, and that's why it returns such error.



no implicit conversion of Symbol into Integer (TypeError)



The result of [ride_data], gives you a string within an array, so, you don't have the ability to pass a symbol, accessing a key as if it'd be a Hash object.


[ride_data]



As your rideshare_data is a hash, containing different values for each key (driver id), you can just iterate over each key values and get the value of each COST key, and do the operation you need:


rideshare_data


rideshare_data.each_with_object() do |(key, value), array|
array << "#key earned #value.sum ride[:COST] total"
end.each total

# DR0001 earned 85 total
# DR0002 earned 75 total



There you iterate over each value in rideshare_data, using an initial empty array as a memo where to store your data, with sum, you collect each value for COST keys and sum them. The each on the final is just to show each value within the resulting array.


rideshare_data


each



Depending on your Ruby version, you might need map and sum instead just sum (value.map ride[:COST] .sum).


value.map ride[:COST] .sum





Maybe just a map rather than each_with_object() here? :)
– SRack
Aug 13 at 12:51


map


each_with_object()





Yes, that's right, something like rideshare_data.map would also work.
– Sebastian Palma
Aug 13 at 14:35


rideshare_data.map






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard