Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Is there anything like Array#Index that takes a block?

Reply
Thread Tools

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

 
 
Lee Griffiths
Guest
Posts: n/a
 
      04-28-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
badboy
Guest
Posts: n/a
 
      04-28-2009
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


 
Reply With Quote
 
 
 
 
Lee Griffiths
Guest
Posts: n/a
 
      04-28-2009
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
badboy
Guest
Posts: n/a
 
      04-28-2009
Lee Griffiths schrieb:
> 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.

hm...oh =/
ruby 1.8.7 (and 1.9.1, too)

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      04-28-2009
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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      04-28-2009
> =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.

 
Reply With Quote
 
badboy
Guest
Posts: n/a
 
      04-28-2009
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


 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      04-28-2009
On Tue, Apr 28, 2009 at 4:19 PM, badboy <(E-Mail Removed)> wrote:
> Robert Dober schrieb:
>>> =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.
>>

> 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.

 
Reply With Quote
 
Roger Pack
Guest
Posts: n/a
 
      02-13-2011
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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
is there anything like GSpot for mp3's mattcar Computer Support 0 11-02-2006 09:34 PM
Is there anything like ASP.NET for Python web apps ? rytoy@yahoo.com Python 1 02-17-2006 10:07 AM
Is there anything like Oracle ADF in other Java IDE tools ? krislioe@gmail.com Java 10 06-30-2005 03:27 PM
is there anything like static constructors or destructors in C++ ? Amit C++ 3 05-06-2005 09:13 AM



Advertisments