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:

- Given scalar s
- Given array a1 = [] which monotonically increases (e.g.

[10,11,12,15,16,…]) - 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