ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: sawadatsuyoshi@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92621] [Ruby trunk Feature#15829] Object#then_if(condition){}
Date: Sat, 11 May 2019 12:11:17 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77980.20190511121116.d2795fb33e2db905@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15829.20190505182618@ruby-lang.org

Issue #15829 has been updated by sawa (Tsuyoshi Sawada).


I have exactly the same concern as nobu. And that problem stems from the fact that, in this proposal, the condition is given as an argument of the method, which means that it has to be evaluated independently of the return value that appears in the middle of the method chain.

That should take us back to #15547, where the condition is proposed to be given as a block (or proc, in a comment).

----------------------------------------
Feature #15829: Object#then_if(condition){}
https://bugs.ruby-lang.org/issues/15829#change-77980

* 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:

```ruby
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:

```ruby
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/

  parent reply	other threads:[~2019-05-11 12:11 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 ` [ruby-core:92560] " foonlyboy
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 ` sawadatsuyoshi [this message]
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-77980.20190511121116.d2795fb33e2db905@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).