Ask A Question

Notifications

You’re not receiving notifications from this thread.

Ruby question - Generating a nested hash with group_by

David Field asked in Ruby

I'm trying to create a nested hash. I have a hash with the year as the key and an amount for that year. hash = {2017 => 55000, 2018 =>55500, 2019 => 60000}

I need to divide the amount for each year by 12 to create a nested has sorted by year and then the month and monthly amount. Once I have this larger hash I will need to go through the hash and delete certain month/amount pairs for each year.

Any recommendations on how best to approach this? I iterated over the key value to create the initial hash (to divide the month values by 12) and then used the group_by feature to create the nested hash, but the hash is too nested (an extra level deep) and I cannot sort out how to get this corrected.

I can be more specific, but wanted to seek out if there were better ways to approach this problem than what I've come up with. I have found tutorials I find on using group_by somewhat limiting. Any help or guidance is greatly appreciated. Thank you in advance.

Reply

Hey David,

What's the final result has that you're trying to accomplish? It would allow for more concrete steps on solving the problem.

Are you trying to make something like:

{ 2017 => [4583.33333333, 4583.33333333, 4583.33333333..., other months], 2018 => ... }

And then delete certain months in the array? or something else?

Reply

Thanks Chris. I was thinking I might need a nested hash because I will need to look up a specific month of a specific year and then delete the value.

hash = {
2017: {month: 1, value: 4583.33}, {month: 2, value: 4583.33}, {month: 3, value: 4583.33},{month: 4, value: 4583.33}, ... },
2018: {month: 1, value: 4583.33}, {month: 2, value: 4583.33}, {month: 3, value: 4583.33},{month: 4, value: 4583.33}, ... },
2019: {month: 1, value: 4583.33}, {month: 2, value: 4583.33}, {month: 3, value: 4583.33},{month: 4, value: 4583.33}, ... },
2020: {month: 1, value: 4583.33}, {month: 2, value: 4583.33}, {month: 3, value: 4583.33},{month: 4, value: 4583.33}, ... }
}

Like I mentioned, I don't even know if the nested hash is needed, but I need to identify specific months to remove them from the further calculation.

Thank you again in advance for any help to put me in a stronger direction.

Reply

A nested hash could work. You could also do an array since you could use the index to determine the month too. For example, 1 -> January, 2 -> February, etc.

How do you currently know which months to remove? Is it a word as a string ("January") or the number of the month like 1,2,3?

You can use I18n.t("date.month_names") in Rails to translate to and from the names and integers.

irb(main):001:0> I18n.t("date.month_names")
=> [nil, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
Reply

Thank you for this Chris, it has given me a way to rethink my approach. I will try it out. I may be back, but thank you very much.

Reply

Sounds good! It sometimes just takes a little bit of mapping out what you've got vs where you need to be and then the pieces start falling together.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.