From: foonlyboy@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92560] [Ruby trunk Feature#15829] Object#then_if(condition){}
Date: Sun, 05 May 2019 18:35:44 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-77923.20190505183544.cbc312e0a5d5da86@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15829.20190505182618@ruby-lang.org
Issue #15829 has been updated by foonlyboy (Eike Dierks).
``` ruby
class Object
def then_if(condition, &block)
if condition
self.then(&block)
else
self
end
end
end
```
----------------------------------------
Feature #15829: Object#then_if(condition){}
https://bugs.ruby-lang.org/issues/15829#change-77923
* Author: foonlyboy (Eike Dierks)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
I'd like to propose some sugar to Object#then
There should be Object#then_if(condition, &block)
The block should only be applied when the condition is true,
otherwise the object should be returned without applying the block.
Rationale:
I frequently use Object#then with Rails to extend queries like this:
```
foo.then {|query|
if(condition)
query.where(zip:zap)
else
query
end
}
```
by using the proposed Object#then_if the example could be simplified to:
```
foo.then_if(condition) {|query|
query.where(zip:zap)
}
```
I believe that this also applies to a lot of other use cases,
i.e. only applying some transformation if some condition is true,
but otherwise leaving the result untouched.
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2019-05-05 18:35 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-15829.20190505182618@ruby-lang.org>
2019-05-05 18:26 ` [ruby-core:92559] [Ruby trunk Feature#15829] Object#then_if(condition){} foonlyboy
2019-05-05 18:35 ` foonlyboy [this message]
2019-05-05 18:48 ` [ruby-core:92561] " foonlyboy
2019-05-05 20:30 ` [ruby-core:92564] " foonlyboy
2019-05-05 20:51 ` [ruby-core:92565] " sawadatsuyoshi
2019-05-11 7:13 ` [ruby-core:92620] " nobu
2019-05-11 12:11 ` [ruby-core:92621] " sawadatsuyoshi
2019-07-29 6:58 ` [ruby-core:93967] [Ruby master " ko1
2019-12-15 8:22 ` [ruby-core:96245] " masafumi.o1988
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-77923.20190505183544.cbc312e0a5d5da86@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).