Return 3rd greatest number given array of numbers - getting noMethoderror

Write a method that takes an array of numbers in. Your method should

return the third greatest number in the array. You may assume that

the array has at least three numbers in it.

def third_greatest(nums)

nums_order =

until nums.length == nil
nums.sort { |a,b| a <=> b
greatest = nums.pop
return nums_order[2]


Test I use:

third_greatest([5, 3, 7])

returns the following:
NoMethodError: undefined method >' for [7]:Array from (irb):107:insort’
from (irb):107:in third_greatest' from (irb):115 from /usr/bin/irb:12:in

I’ve tried the sort without ‘a,b’ conditions …and also playing
around with until condition …i.e. while nums.length > 0…until
nums.length == 0 (don’t know if those make sense, however, because
wouldn’t empty array == nil?)

Little help please and thanks!

how about this?

It could also be done like this:

def third_greatest(array)

return array.sort[2]


third_greatest([5, 3, 7])

Yes, a good option.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs