> > 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>'


Outlook habitually capitalises the first character in every line.  I missed one. You know very well from the previous context we were talking about a parameter name, not a class.


> 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>'


    [1] pry(main)> limit = 10; [-1, -20, -2].max_by{|x| x < limit ? x : nil }
    => -1
    [2] pry(main)> limit = 10; [-1, 20, -2].max_by{|x| x < 10 ? x : -9999999 }
    => -1

See my previous answer about my example code not working against all test cases?


Please try not to act like an idiot.  This is not a competition where you can win points by trying to prove other people are wrong, and I'm not interested in having an argument with you.

You found an interesting edge case in max_by that I was unaware of.  What a shame we can't have a pleasant discussion about that now...


Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

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