From: Stefano Crocco Date: 2007-03-09T17:13:49+09:00 Subject: Re: 2 lines in one... Alle venerd� 9 marzo 2007, Josselin ha scritto: > Could I write it in just 1 line ? > > find the first value greater than d, in a list (special one : each > value is the double of the previous...) > then give me the index of this value (with special case ...) > > i = > ([0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].selec >t {|v| v if v >= d }).first > > zl = d <0.5 ? 0 : > [0.5,1.0,2.0,4.0,8.0,16.0,32.0,64.0,128.0,256.0,512.0,1024.0,2048.0].index( >i) > > joss What about this? a.each_index{|i| break i if a[i] > d} The only problem is that it returns a if no element greater than d is found. You could turn it into a method, though: def find_greater array, value array.each_index{|i| return i if array[i] > value} nil end