Actions
Feature #12318
closedReturning the evaluated value of a block
Feature #12318:
Returning the evaluated value of a block
Status:
Closed
Assignee:
-
Target version:
-
Description
I often achieve an element using an iterator with a block, and then apply the same/a similar block to the element I get. Examples are:
[7, 8, 9]
.max_by{|e| e % 3}
.tap{|e| break e % 3}
# => 2
["foo", "bar", "baz"]
.find{|e| e[/(.)\1/]}
.tap{|e| break e[/(.)\1/]}
# => "oo"
I would like a method on Enumerator that returns the result of the block rather than the original element in the iterator. Not sure about the name, but if I call it and_return temporary, it should look like:
[7, 8, 9]
.max_by.and_return{|e| e % 3}
# => 2
["foo", "bar", "baz"]
.find.and_return{|e| e[/(.)\1/]}
# => "oo"
Updated by nobu (Nobuyoshi Nakada) over 9 years ago
It doesn't seem generic enough to be defined in Enumerator.
The receiver enumerator may return different object than its elements nor the results of the given block, whereas that and_return has to manage the association of them.
Why not simpler:
[7, 8, 9].map {|e| e % 3}.max
# => 2
["foo", "bar", "baz"].find {|e| break e if e = e[/(.)\1/]}
# => "oo"
or
["foo", "bar", "baz"].grep(/(.)\1/) {break $&}
# => "oo"
Updated by avit (Andrew Vit) almost 8 years ago
This is now implemented as yield_self
Close please?
Updated by shyouhei (Shyouhei Urabe) almost 8 years ago
- Status changed from Open to Closed
avit (Andrew Vit) wrote:
This is now implemented as
yield_selfClose please?
Yes. Closing.
Actions