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?
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.
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 RDoc Documentation
claims that Array#index only has a single implementation.
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.
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
yes… it’s called Array#index scnr
this. I’m using Ruby 1.8.6. Additionally RDoc Documentation
claims that Array#index only has a single implementation.
hm…oh =/
ruby 1.8.7 (and 1.9.1, too)
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.