Forum: Ruby Ruby feature request - enum.detect_index

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.
jonT (Guest)
on 2006-02-05 16:43
(Received via mailing list)
Hi,

I'd like to propose a new addition to the enum module.

detect works great but doesn't supply the index of the match. It would
be nice to have a method detect_index that returned the index rather
than the object.

Use example:

irb(main):001:0> foo =`uptime`.strip.split(' ')
=> ["14:36:06", "up", "157", "days,", "16:59,", "35", "users,", "load",
"average:", "0.54,", "0.31,", "0.27"]
irb(main):002:0> bar=nil
=> nil
irb(main):003:0> foo.each_with_index {|o,i| if o=="users,"; bar=i;
break; end;}
=> nil
irb(main):004:0> foo[2..bar-2]
=> ["157", "days,", "16:59,"]

Lines 2 to 4 would be expressed in a much more concise manner, i.e.

irb(main):001:0> foo =`uptime`.strip.split(' ')
=> ["14:36:06", "up", "157", "days,", "16:59,", "35", "users,", "load",
"average:", "0.54,", "0.31,", "0.27"]
irb(main):002:0> foo[2..foo.detect_index {|o| o=="users,"}-2]
=> ["157", "days,", "16:59,"]

Thoughts?

Jon T
Levin A. (Guest)
on 2006-02-05 17:05
(Received via mailing list)
On 2/5/06, jonT <removed_email_address@domain.invalid> wrote:

> I'd like to propose a new addition to the enum module.
>
> detect works great but doesn't supply the index of the match. It would
> be nice to have a method detect_index that returned the index rather
> than the object.

You can do it with enumerator:

  require 'enumerator'
  [1,2,3,4,5,6].to_enum(:each_with_index).detect {|elem, index| elem
== 4} #=> [4,3]

(enumerator is probably the one library in the stdlib I use most.  It
is amazingly useful)

-Levin
Yukihiro M. (Guest)
on 2006-02-05 17:20
(Received via mailing list)
Hi,

In message "Re: Ruby feature request - enum.detect_index"
    on Sun, 5 Feb 2006 23:43:19 +0900, "jonT" 
<removed_email_address@domain.invalid> writes:

|detect works great but doesn't supply the index of the match. It would
|be nice to have a method detect_index that returned the index rather
|than the object.

Nice idea.  I'd rather name it 'find_index' though.

							matz.
Ross B. (Guest)
on 2006-02-05 17:44
(Received via mailing list)
On Sun, 05 Feb 2006 14:41:21 -0000, jonT <removed_email_address@domain.invalid> 
wrote:

> "average:", "0.54,", "0.31,", "0.27"]
> irb(main):001:0> foo =`uptime`.strip.split(' ')
> => ["14:36:06", "up", "157", "days,", "16:59,", "35", "users,", "load",
> "average:", "0.54,", "0.31,", "0.27"]
> irb(main):002:0> foo[2..foo.detect_index {|o| o=="users,"}-2]
> => ["157", "days,", "16:59,"]
>

For this case, this might work?

	foo[2..foo.index("users,")-2]
	# => ["1", "day,", "14:18,"]

More generally, I'd personally like a block on 'index', such that:

	foo.index("users,")
	# => 6

	foo.index { |s| s == "users," }
	# => 6

Just MHO.
Robert K. (Guest)
on 2006-02-05 17:50
(Received via mailing list)
Yukihiro M. <removed_email_address@domain.invalid> wrote:
> Hi,
>
> In message "Re: Ruby feature request - enum.detect_index"
>    on Sun, 5 Feb 2006 23:43:19 +0900, "jonT" <removed_email_address@domain.invalid> 
writes:
>
>> detect works great but doesn't supply the index of the match. It
>> would be nice to have a method detect_index that returned the index
>> rather than the object.
>
> Nice idea.  I'd rather name it 'find_index' though.

Don't we have that already?

>> %w{foo bar baz}.index "baz"
=> 2

Or am I missing something?

Kind regards

    robert
Yukihiro M. (Guest)
on 2006-02-05 17:50
(Received via mailing list)
Hi,

In message "Re: Ruby feature request - enum.detect_index"
    on Mon, 6 Feb 2006 00:43:20 +0900, "Ross B."
<removed_email_address@domain.invalid> writes:

|More generally, I'd personally like a block on 'index', such that:
|
|	foo.index("users,")
|	# => 6
|
|	foo.index { |s| s == "users," }
|	# => 6

It does so in 1.9.

							matz.
James G. (Guest)
on 2006-02-05 17:53
(Received via mailing list)
On Feb 5, 2006, at 9:48 AM, Robert K. wrote:

>
>>> %w{foo bar baz}.index "baz"
> => 2
>
> Or am I missing something?

You can not use a block of Ruby code to select the item you would
like an index for, in this example.

James Edward G. II
Ross B. (Guest)
on 2006-02-05 18:14
(Received via mailing list)
On Sun, 05 Feb 2006 15:50:22 -0000, Yukihiro M.
<removed_email_address@domain.invalid> wrote:

> |
> |	foo.index { |s| s == "users," }
> |	# => 6
>
> It does so in 1.9.
>

Cool, so it does :) I need to update my snapshot more often I guess ...

Thanks,
Robert K. (Guest)
on 2006-02-05 18:14
(Received via mailing list)
James Edward G. II <removed_email_address@domain.invalid> wrote:
>>
>> Don't we have that already?
>>
>>>> %w{foo bar baz}.index "baz"
>> => 2
>>
>> Or am I missing something?
>
> You can not use a block of Ruby code to select the item you would
> like an index for, in this example.

Good point.  In that case I'd just change the implementation of #index
to
either accept an argument or a block.  This cannot break old code
because at
the moment blocks are not permitted / ignored.

Kind regards

    robert
jonT (Guest)
on 2006-02-05 18:38
(Received via mailing list)
> |More generally, I'd personally like a block on 'index', such that:
> |
> |  foo.index("users,")
> |  # => 6
> |
> |  foo.index { |s| s == "users," }
> |  # => 6

> It does so in 1.9.

Ah! in that case my detect_index suggestion was rather unnecessary
[apologies - I also am guilty of running an ancient version of 1.9
(ubuntu's is ruby 1.9.0 (2005-06-23) (!)]

Jon T
This topic is locked and can not be replied to.