Rob Redmon wrote:
> How do I find matching expressions and return the match and the index of
> the match?  Actually, I'd be happy with just the index.
> 
> Basically, I want the Ruby way to:
> 1) Given scalar s
> 2) Given array a1 = [] which monotonically increases (e.g.
> [10,11,12,15,16,..])
> 3) 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.
> 
> R


Hi friend,
            I think that in find_all method, we don't have any provision 
to
extract indices alone. But there could be enormous way to achieve your 
target.
I suggest you the below code:

irb(main):064:0> a=[10, 20, 30, 40, 50, 60]
=> [10, 20, 30, 40, 50, 60]
irb(main):065:0> c=[];a.each_index{|i| a[i]>20?c<<i:nil }
=> [10, 20, 30, 40, 50, 60]

-- 
Posted via http://www.ruby-forum.com/.