Forum: Ruby Is there anything like Array#Index that takes a block?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
984b257729902b4ab1a32dedd3df899c?d=identicon&s=25 Pod Pod (pod)
on 2009-04-28 14:19
I would like to avoid doing :
Array.index(Enumerable.find { |blarg| blarg =~ /blah/ })

as that traverses the array twice. Is there any built in method that
does this for me? That is, it's a method that takes a block and returns
the index of the first element for which this block returns true?

Thanks
245a6d22816ecaeac1c9080ad183b859?d=identicon&s=25 badboy (Guest)
on 2009-04-28 14:23
(Received via mailing list)
Lee Griffiths schrieb:
> I would like to avoid doing :
> Array.index(Enumerable.find { |blarg| blarg =~ /blah/ })
>
> as that traverses the array twice. Is there any built in method that
> does this for me? That is, it's a method that takes a block and returns
> the index of the first element for which this block returns true?
>
> Thanks
yes... it's called Array#index *scnr*
ri says:
     array.index(obj)           ->  int or nil
     array.index {|item| block} ->  int or nil
  ...
  If a block is given instead of an argument, returns first
     object for which _block_ is true.

so...just use #index with a blocck
984b257729902b4ab1a32dedd3df899c?d=identicon&s=25 Pod Pod (pod)
on 2009-04-28 14:55
badboy wrote:
> Lee Griffiths schrieb:
>> I would like to avoid doing :
>> Array.index(Enumerable.find { |blarg| blarg =~ /blah/ })
>>
>> as that traverses the array twice. Is there any built in method that
>> does this for me? That is, it's a method that takes a block and returns
>> the index of the first element for which this block returns true?
>>
>> Thanks
> yes... it's called Array#index *scnr*
> ri says:
>      array.index(obj)           ->  int or nil
>      array.index {|item| block} ->  int or nil
>   ...
>   If a block is given instead of an argument, returns first
>      object for which _block_ is true.
>
> so...just use #index with a blocck

Which version of Ruby is that for? My ri, irb and ruby don't confirm
this. I'm using Ruby 1.8.6. Additionally http://www.ruby-doc.org/core/
claims that Array#index only has a single implementation.
245a6d22816ecaeac1c9080ad183b859?d=identicon&s=25 badboy (Guest)
on 2009-04-28 14:57
(Received via mailing list)
Lee Griffiths schrieb:
>> yes... it's called Array#index *scnr*
> this. I'm using Ruby 1.8.6. Additionally http://www.ruby-doc.org/core/
> claims that Array#index only has a single implementation.
hm...oh =/
ruby 1.8.7 (and 1.9.1, too)
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-28 15:20
arr = %w{apple banana blarg blueberry}

pos = nil
arr.each_with_index do |str, i|
  if str =~ /bl/
    pos = i
    break
  end

end

puts pos

--output:--
2
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-28 16:17
(Received via mailing list)
>  If a block is given instead of an argument, returns first
>     object for which _block_ is true.
I could not believe it says that, probably should provide a patch.

OP do not worry, of course it returns the index and not the object.
HTH
R.
245a6d22816ecaeac1c9080ad183b859?d=identicon&s=25 badboy (Guest)
on 2009-04-28 16:20
(Received via mailing list)
Robert Dober schrieb:
>>  If a block is given instead of an argument, returns first
>>     object for which _block_ is true.
> I could not believe it says that, probably should provide a patch.
>
> OP do not worry, of course it returns the index and not the object.
> HTH
> R.
>
ah! yeah, I saw that, too but missed to mention it. It's just a
documentation mistake
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-04-28 16:51
(Received via mailing list)
On Tue, Apr 28, 2009 at 4:19 PM, badboy <badboy@heartofgold.co.cc>
wrote:
> documentation mistake
yes indeed, but do you not think this is bad? I had to fire up irb to
check so have done billions of billions of Ruby users in the
universe...
Think big!
R.
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2011-02-13 02:38
For followers, I believe the acceptable backport for 1.8.6 is:


if RUBY_VERSION < '1.8.7'
 class Array
   alias original_index index
   def index *args

     if args.length > 0
       return original_index(*args)
     else
        pos = nil
        each_with_index do |element, i|
          if yield(element)
            return i
          end
        end
       return nil
     end
    end
  end
end
This topic is locked and can not be replied to.