From: Bil Kleb Date: 2006-05-10T20:18:09+09:00 Subject: Re: Finding an interval in a sorted array? Sergey Volkov wrote: > class Array > def interval_containing( x ) > # elegant code goes here > [select{|e|e<=x}[-1], select{|e|e>=x}[0]].compact.uniq > end > end My officemate prompted this question. Here's his "final answer": def interval_containing x lower, upper = partition{|i| i <= x} [lower.last, upper.first] end This leaves a nil in one of the slots if the interval is off one of the ends, which might be what I want. To pass the tests, the return value should be [lower.last, upper.first].uniq Thanks for all the ideas, -- Bil http://fun3d.larc.nasa.gov