Project

General

Profile

Actions

Feature #12318

closed

Returning the evaluated value of a block

Added by sawa (Tsuyoshi Sawada) over 8 years ago. Updated almost 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:75183]

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"
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0