From: lasitha Date: 2009-04-24T14:39:24+09:00 Subject: Re: Array.find with index On Fri, Apr 24, 2009 at 9:38 AM, Rob Redmon wrote: > [...] > Find the two elements in a1 who are nearest neighbors to the scalar > s.  I need the indices more than the values. > > Clearly, I can find the nearest values with this: >>> [10,20,30,40,50].find_all{|item| item >= 25 }.first > => 30 >>> [10,20,30,40,50].find_all{|item| item <= 25 }.last > => 20 > > How do I get the matched indices?  That is, without ugly loops. #index and #rindex in their block forms come to mind: http://ruby-doc.org/core-1.9/classes/Array.html#M001974 cheers, lasitha