On 26.11.2006 14:59, Josselin wrote:
> with :
> array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 0, 0, 
> 0, 0, 0, 0, 0]
> 
> I wrote :
> array.index(array.detect {|x| x > 0})    => 15
> 
> is there a better and simpler way to do it ?
> thanks

Did we have a solution with #inject already?  In case we didn't:

irb(main):001:0> require 'enumerator'
=> true
irb(main):002:0> a=Array.new(10,0) << 666 << 0
=> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 666, 0]
irb(main):003:0> a.to_enum(:each_with_index).inject(nil) {|pp,(x,i)| 
break i unless x == 0}
=> 10
irb(main):004:0> a[0..5]
=> [0, 0, 0, 0, 0, 0]
irb(main):005:0> a[0..5].to_enum(:each_with_index).inject(nil) 
{|pp,(x,i)| break i unless x == 0}
=> nil

Kind regards

	robert