0

What I have:

hash = {id =>[string, number], id =>[string, number]}

I need to get the max value of number. I have been able to do this, but when I puts it. I get:

id
string
number

What I need please

id string number

This is what I've tried:

This brings the max value to the top of list, but I need to exclude the rest of the list.

hash.each{|x, y| puts "#{x} #{y[0]} #{y[1]}"}.max

This returns the max value but displays it vertically

puts hash.max_by{|y| "#{y}"} 

I have tried numerous other things and am having a hard time wrapping my head around this one.

Not sure if it matters but I am read this in from a file into a hash, the number is a float

2
  • I expect you realize that hashes have unique keys, and most readers probably know what your hash looks like, but you should write it like (for example) this: hash = {id1 =>[string1, number1], id2 =>[string2, number2]}. Commented Nov 18, 2013 at 4:27
  • K, thanks for the tip Commented Nov 18, 2013 at 5:25

2 Answers 2

4

The max here doesn’t do anything (since it is called on hash and its return value never used):

hash.each{|x, y| puts "#{x} #{y[0]} #{y[1]}"}.max

This is the same as doing puts on an array (since that’s what max_by returns), which prints each element on a separate line. You’re also unnecessarily converting your number to a string (which can result in unexpected comparison results):

puts hash.max_by{|y| "#{y}"}

Instead let’s just get the max key/value pair:

max = hash.max_by { |id, (string, number)| number }
#=> ["the-id", ["the-string", 3.14]]

Now we can flatten and join the array before puts-ing it:

puts max.flatten.join(' ')
# prints: the-id the-string 3.14
Sign up to request clarification or add additional context in comments.

6 Comments

Wow! I didn't realize that in the block I could do |id,(string, number)|. Only problem is that I added new items with a higher number but with a lower index to the hash and what it is doing is giving me the max index instead of the value.
Sorry I mean with a lower "id" and a higher "number"
Thanks man you really helped me. I didn't realize that I could put the |id,(string, number)| in block like that. So this is what I did. hash.each do |id,(string, number)| max = "#{id}. #{string}, #{number}" end puts max
You said “max value of number“ in your question, but your comment seems to contradict that. Stop using each when you can use a higher-order function. Your each in your comment will just get the last key inserted into hash (along with its value), not necessarily the max of anything.
@user2963093 No, it really doesn’t. See ideone.com/SaMvu4 for example. Note how the “max” value is in fact not in any way the max.
|
0

I would re-arrange the hash with number as the key, then use sort_by(): http://www.rubyinside.com/how-to/ruby-sort-hash

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.