On Mon, Dec 5, 2016 at 2:15 PM, Andy Jones <Andy.Jones / jameshall.co.uk> wrote:
>> No, this does not work reliably (e.g. when there are only negative
>> numbers in array).
>
> Okay, then:
>
>     Array.max_by{|x| x < limit ? x : nil }

irb(main):016:0> Array.max_by{|x| x < limit ? x : nil }
NoMethodError: undefined method `max_by' for Array:Class
        from (irb):16
        from /usr/bin/irb:11:in `<main>'

> Nothing wrong with max_by AFAIK.

irb(main):015:0> limit = 10; [-1, 20, -2].max_by{|x| x < limit ? x : nil }
ArgumentError: comparison of NilClass with -1 failed
        from (irb):15:in `each'
        from (irb):15:in `max_by'
        from (irb):15
        from /usr/bin/irb:11:in `<main>'

> None of my example code posted here is fully tested against all possible data sets.  I'm sorry if you assumed it was.

Then why do you post them, especially since testing is so easy?

Regards

robert

-- 
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>