Project

General

Profile

Feature #13172

Method that yields object to block and returns result

Added by Nondv (Dmitriy Non) 6 months ago. Updated 3 months 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!' : ':(' }
==> ":("
method_pass.patch (904 Bytes) method_pass.patch Nondv (Dmitriy Non), 02/17/2017 11:55 AM

Related issues

Is duplicate of Ruby trunk - Feature #10095: Object#asClosed
Is duplicate of Ruby trunk - Feature #11717: Object#trap -- pass object to block and return resultClosed
Is duplicate of Ruby trunk - Feature #12760: Optional block argument for `itself`Closed

History

#1 [ruby-core:79322] Updated by Nondv (Dmitriy Non) 6 months ago

  • Description updated (diff)

#2 [ruby-core:79323] Updated by Nondv (Dmitriy Non) 6 months ago

  • Subject changed from Object#pass (yield object to block and return result) to Method that yields object to block and returns result

#3 [ruby-core:79324] Updated by Nondv (Dmitriy Non) 6 months ago

Oh, I forgot to notice. There's #instance_eval method, which do the job, but it evaluates block in object context, so it doesn't fit

#4 [ruby-core:79325] Updated by Nondv (Dmitriy Non) 6 months ago

  • Tracker changed from Bug to Feature

#6 [ruby-core:79327] Updated by Nondv (Dmitriy Non) 6 months ago

Matthew Kerwin wrote:

Well, obviously I can't use searching:D
So, what about naming?

#12760 suggests yield_self name, but imho it's too long.

Check https://github.com/ruby/ruby/pull/1521 pls.

#7 Updated by nobu (Nobuyoshi Nakada) 6 months ago

#8 Updated by nobu (Nobuyoshi Nakada) 6 months ago

  • Is duplicate of Feature #11717: Object#trap -- pass object to block and return result added

#9 Updated by nobu (Nobuyoshi Nakada) 6 months ago

  • Is duplicate of Feature #12760: Optional block argument for `itself` added

#10 [ruby-core:79328] Updated by Nondv (Dmitriy Non) 6 months ago

  • Description updated (diff)

#11 [ruby-core:79329] Updated by Nondv (Dmitriy Non) 6 months ago

  • Description updated (diff)

#12 [ruby-core:79330] Updated by Nondv (Dmitriy Non) 6 months ago

  • Description updated (diff)

#13 [ruby-core:79331] Updated by Nondv (Dmitriy Non) 6 months ago

  • Description updated (diff)

#15 [ruby-core:80722] Updated by matz (Yukihiro Matsumoto) 3 months ago

I don't think "pass" is a good name for it.

Matz.

#16 Updated by nobu (Nobuyoshi Nakada) 3 months ago

  • Status changed from Open to Closed

Applied in changeset trunk|r58528.


object.c: Kernel#yield_self

  • object.c (rb_obj_yield_self): new method which yields the receiver and returns the result. [Feature #6721]

Also available in: Atom PDF