Question

I have only just started to learn Ruby hoping one of you have some time to explain something to a complete newbie. I have been learning from Treehouse and Lynda (any other suggestions) and want to start testing what i know to write a small sports tipping app for my friends.

basically my theory is to 1. parse some data from an official site 2. get this into the format of a hash 3. iterate over the hash to see which score (value) is higher and which key (team) that is associated with to get a winner.

this is the core of functionality, however there is obviously code required around;

  1. signing up and manage users
  2. getting tips
  3. giving results (and winners)

My first question is around iterating over a hash to compare the scores and get the winner. what would be the most beautiful code to achieve this.

Hope you can help and thank you for your help its really appreciated, hope one day to be here with some skills returning the favour !

Was it helpful?

Solution

No offense but this post has a lot of explanation for what is really a very simple question: how to find the largest value in a hash.

That being said, you could do scores.max_by{|key,val| val} on a hash called scores. This is assuming the scores are integers, not strings. If the scores are strings you could do scores.max_by{|key,val| val.to_i} instead. That will return an array with the key (team name) as the first item, and the value (the score) as the second item.

See the original question from StackOverflow I posted above for more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top