It seems as if max_by() deserves a mention here:

    def first_under(array, limit)
      array.max_by{|e| e < limit ? e : 0 }
    end

That will get you the highest number in the array less than the limit.  If you want the first number in the array less than the limit, as already covered, that”Ēs just find():

    def first_under(array, limit)
      array.find{|e| e < limit }
    end


From: ruby-talk [mailto:ruby-talk-bounces / ruby-lang.org] On Behalf Of Matthew Kerwin
Sent: 04 December 2016 11:24 pm
To: Ruby users
Subject: Re: largest number in an array below a given value



On 5 December 2016 at 07:07, Micky Scandal <mickyscandal / gmail.com<mailto:mickyscandal / gmail.com>> wrote:
huh... it looks like I was confusing myself and doing way more work than I need to again. when it said the first number less than... i took that to mean the first number chronologically not the first number from left to right. but I suppose that does make sense. thanks for clarifying that for me, and sorry for the confusion


If it helps keep these sort of things clear:

An array (even outside of programming) is "a systematic or ordered arrangement of similar objects." In programming 'systematic' and 'ordered' mean pretty much the same thing, and with all ordered collections, "first" always means "first according to the order of the collection."

It doesn't matter if the ordering is by-index (as an Array), or by-insertion (as a Hash), or by-smallest-to-largest (if you built a sorting tree object) or any other system you can think of.

Cheers
--
  Matthew Kerwin
  http://matthew.kerwin.net.au/


Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>
(supressed text/html)
Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>