From: Robert Klemme Date: 2007-03-09T18:32:50+09:00 Subject: Re: 2 lines in one... 2007/3/9, Josselin : > 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].select > {|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) irb(main):015:0> arr=[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] => [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] irb(main):016:0> idx = arr.to_enum(:each_with_index).inject(nil) {|x,(e,i)| break i if e > 100} => 8 irb(main):017:0> idx = arr.to_enum(:each_with_index).inject(nil) {|x,(e,i)| break i if e > 1000000} => nil Kind regards robert -- Have a look: http://www.flickr.com/photos/fussel-foto/