From: Jimmy Kofler Date: 2007-08-24T20:56:06+09:00 Subject: Re: Selecting indices from an array > Posted by Ronald Fischer (Guest) on 23.08.2007 12:59 > > ... > Is there in Ruby a function similar select, where I also get the array element passed through, kind of: > > a.select_index {|index,value| value?~/g$/} > > or do I have to write my own here? I don't want to reinvent the wheel.... > > Ronald How about: module Enumerable def select_index index = -1 (block_given? && self.class == Range || self.class == Array) ? collect { |x| index += 1; yield(x,index) }.compact : self end end p ("a".."n").select_index { |x,i| i if x =~ /[c-g]/ } => [2, 3, 4, 5, 6] Cheers, j.k. -- Posted via http://www.ruby-forum.com/.