Project

General

Profile

Actions

Feature #13172

closed

Method that yields object to block and returns result

Added by Nondv (Dmitry Non) over 4 years ago. Updated over 4 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:79321]

Description

Hi everyone!

There's a #tap method, that yields reciever to block and returns reciever itself.
I would like to see method, which would return yield's result.

some_object.some_method.pass { |x| x && another_object.another_method(x) }

I've seen many code samples that had a some "middle-variables" to store #some_method result.
Those variables are useless after few lines of code, so it would be great to create them in a limited scope (block).

P.S. I am very sorry for my english. I hope you understood me:D
P.P.S. I think there should be a better name than pass for such method

UPD. I've created a PR to see if #pass fits: https://github.com/ruby/ruby/pull/1521

$ irb
>> Time.now.pass { |t| t.sunday? ? 'Yay!' : ':(' }
==> ":("

Files

method_pass.patch (904 Bytes) method_pass.patch Nondv (Dmitry Non), 02/17/2017 11:55 AM

Related issues

Is duplicate of Ruby master - Feature #10095: Object#asClosedActions
Is duplicate of Ruby master - Feature #11717: Object#trap -- pass object to block and return resultClosedActions
Is duplicate of Ruby master - Feature #12760: Optional block argument for `itself`ClosedActions
Actions

Also available in: Atom PDF