ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: masafumi.o1988@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:96245] [Ruby master Feature#15829] Object#then_if(condition){}
Date: Sun, 15 Dec 2019 08:22:37 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-83137.20191215082237.635745a2ea93d673@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15829.20190505182618@ruby-lang.org

Issue #15829 has been updated by okuramasafumi (Masafumi OKURA).


How about `call`ing a condition object if it's callable instead of simply using value as a condition?

```
class Object
  def then_if(condition, &block)
    if condition
      if (condition.respond_to?(:call) && condition.call(self)) || condition
        self.then(&block)
      end
    else
      self
    end
  end
end
```

Then we can do something like this:

```
'str'.then_if(Proc.new {|str| str.downcase == str}) {|str| str.upcase}
```

I know it's complicated and not beautiful, but it's an idea anyway.

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

* Author: foonlyboy (Eike Dierks)
* Status: Rejected
* 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-12-15  8:22 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 ` [ruby-core:92621] " sawadatsuyoshi
2019-07-29  6:58 ` [ruby-core:93967] [Ruby master " ko1
2019-12-15  8:22 ` masafumi.o1988 [this message]

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-83137.20191215082237.635745a2ea93d673@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).