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 = Array.new

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

end

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?
nums.sort[-3]

It could also be done like this:

def third_greatest(array)

return array.sort[2]

end

third_greatest([5, 3, 7])

http://antipark.ru/dopusk-sro-stroiteley-vstuplenie-np.html

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