Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Object#pass method #1521

Closed
wants to merge 1 commit into from
Closed

add Object#pass method #1521

wants to merge 1 commit into from

Conversation

Nondv
Copy link

@Nondv Nondv commented Jan 31, 2017

https://bugs.ruby-lang.org/issues/13172

Instead of #yield_self from https://bugs.ruby-lang.org/issues/12760

Just to see if it fits

@Nondv
Copy link
Author

Nondv commented Jan 31, 2017

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

@DmitryDrobotov
Copy link

I don't see a real reason to have such method because

Time.now.sunday? ? 'Yay!' : ':('

gives the same result. Maybe, could you provide more explicit example where it will add cool syntactic sugar or better code efficiency?

@Nondv
Copy link
Author

Nondv commented Mar 2, 2017

@DmitryDrobotov I've given 2 links to Redmine. Help yourself, please:)
People want it.

In the simplest case it helps handle long-name variables:

if application_default_user.male?
  "Mr. #{application_default_user.name}"
else
  "Ms. #{application_default_user.name}"
end

# vs
application_default_user.pass { |u| u.male? ? "Mr. #{u.name}" : "Ms. #{u.name}" }
# or 
application_default_user.yield_self { |u| u.male? ? "Mr. #{u.name}" : "Ms. #{u.name}" }

Of course, you can have complicated logic in a multiline block with many temporary variables (and they will exist only in block scope!) so it should help very much.

P.S. application_default_user isn't longest name by the way.

@znz
Copy link
Member

znz commented May 2, 2017

This pull request can close, because yield_self added.

(I don't have permission to close.)

@nobu nobu closed this May 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants