Ruby feature request - enum.detect_index

J

jonT

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
 
L

Levin Alexander

T24gMi81LzA2LCBqb25UIDxqQHRpcHBlbGwuY29tPiB3cm90ZToKCj4gSSdkIGxpa2UgdG8gcHJv
cG9zZSBhIG5ldyBhZGRpdGlvbiB0byB0aGUgZW51bSBtb2R1bGUuCj4KPiBkZXRlY3Qgd29ya3Mg
Z3JlYXQgYnV0IGRvZXNuJ3Qgc3VwcGx5IHRoZSBpbmRleCBvZiB0aGUgbWF0Y2guIEl0IHdvdWxk
Cj4gYmUgbmljZSB0byBoYXZlIGEgbWV0aG9kIGRldGVjdF9pbmRleCB0aGF0IHJldHVybmVkIHRo
ZSBpbmRleCByYXRoZXIKPiB0aGFuIHRoZSBvYmplY3QuCgpZb3UgY2FuIGRvIGl0IHdpdGggZW51
bWVyYXRvcjoKCiAgcmVxdWlyZSAnZW51bWVyYXRvcicKICBbMSwyLDMsNCw1LDZdLnRvX2VudW0o
OmVhY2hfd2l0aF9pbmRleCkuZGV0ZWN0IHt8ZWxlbSwgaW5kZXh8IGVsZW0KPT0gNH0gIz0+IFs0
LDNdCgooZW51bWVyYXRvciBpcyBwcm9iYWJseSB0aGUgb25lIGxpYnJhcnkgaW4gdGhlIHN0ZGxp
YiBJIHVzZSBtb3N0LiAgSXQKaXMgYW1hemluZ2x5IHVzZWZ1bCkKCi1MZXZpbgo=
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Ruby feature request - enum.detect_index"

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

Ross Bamford

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,"]

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

Robert Klemme

Yukihiro Matsumoto said:
Hi,

In message "Re: Ruby feature request - enum.detect_index"


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

Don't we have that already?
=> 2

Or am I missing something?

Kind regards

robert
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Ruby feature request - enum.detect_index"

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

James Edward Gray II

Don't we have that already?

=> 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 Gray II
 
R

Ross Bamford

Hi,

In message "Re: Ruby feature request - enum.detect_index"
on Mon, 6 Feb 2006 00:43:20 +0900, "Ross Bamford"

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

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

Thanks,
 
R

Robert Klemme

James Edward Gray II said:
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
 
J

jonT

|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
 

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
474,202
Messages
2,571,057
Members
47,661
Latest member
sxarexu

Latest Threads

Top