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

L

Lee Griffiths

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
 
B

badboy

Lee said:
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
 
L

Lee Griffiths

badboy said:
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.
 
7

7stud --

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
 
R

Robert Dober

=A0If a block is given instead of an argument, returns first
=A0 =A0 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.
 
B

badboy

Robert said:
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
 
R

Robert Dober

ah! yeah, I saw that, too but missed to mention it. It's just a
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.
 
R

Roger Pack

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top