ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:92559] [Ruby trunk Feature#15829] Object#then_if(condition){}
       [not found] <redmine.issue-15829.20190505182618@ruby-lang.org>
@ 2019-05-05 18:26 ` foonlyboy
  2019-05-05 18:35 ` [ruby-core:92560] " foonlyboy
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: foonlyboy @ 2019-05-05 18:26 UTC (permalink / raw)
  To: ruby-core

Issue #15829 has been reported by foonlyboy (Eike Dierks).

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

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:92560] [Ruby trunk Feature#15829] Object#then_if(condition){}
       [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
  2019-05-05 18:48 ` [ruby-core:92561] " foonlyboy
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: foonlyboy @ 2019-05-05 18:35 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:92561] [Ruby trunk Feature#15829] Object#then_if(condition){}
       [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 ` foonlyboy
  2019-05-05 20:30 ` [ruby-core:92564] " foonlyboy
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: foonlyboy @ 2019-05-05 18:48 UTC (permalink / raw)
  To: ruby-core

Issue #15829 has been updated by foonlyboy (Eike Dierks).


A (somehow contrived) example:

``` ruby
[1,2,3,4,5].map{|n| n.then_if(n.even?){'even'}}
=> [1, "even", 3, "even", 5]
```




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

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:92564] [Ruby trunk Feature#15829] Object#then_if(condition){}
       [not found] <redmine.issue-15829.20190505182618@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-05-05 18:48 ` [ruby-core:92561] " foonlyboy
@ 2019-05-05 20:30 ` foonlyboy
  2019-05-05 20:51 ` [ruby-core:92565] " sawadatsuyoshi
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: foonlyboy @ 2019-05-05 20:30 UTC (permalink / raw)
  To: ruby-core

Issue #15829 has been updated by foonlyboy (Eike Dierks).


A more complex example:

``` ruby
    scope :blacklisted_at, -> (seller=nil, flag=true) {
      then_if(! flag.nil?) {
        joins(:disk_seller_maps)
        .then_if(seller) {|q|
          q.where(disk_seller_map:{seller_id:seller})
        }
        .then_if(! flag) {|q|
          all.where.not(id: q)
        }
      }
    }

```

Not really sure if this does what I mean,
but it looks a lot more concise now.


Before that all that cases would have expanded to 6 cases,
but now it looks a lot more concise.

- then_if(! flag.nil?) shields from flag.nil?
- then_if(seller) only applies if a seller was given
- then_if(! flag) negates the query

This brings the code down from 2x3=6 variants,
to only two code blocks (the second one being trivial in this case)

















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

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:92565] [Ruby trunk Feature#15829] Object#then_if(condition){}
       [not found] <redmine.issue-15829.20190505182618@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-05-05 20:30 ` [ruby-core:92564] " foonlyboy
@ 2019-05-05 20:51 ` sawadatsuyoshi
  2019-05-11  7:13 ` [ruby-core:92620] " nobu
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: sawadatsuyoshi @ 2019-05-05 20:51 UTC (permalink / raw)
  To: ruby-core

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


Related to https://bugs.ruby-lang.org/issues/15557.

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

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:92620] [Ruby trunk Feature#15829] Object#then_if(condition){}
       [not found] <redmine.issue-15829.20190505182618@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2019-05-05 20:51 ` [ruby-core:92565] " sawadatsuyoshi
@ 2019-05-11  7:13 ` nobu
  2019-05-11 12:11 ` [ruby-core:92621] " sawadatsuyoshi
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: nobu @ 2019-05-11  7:13 UTC (permalink / raw)
  To: ruby-core

Issue #15829 has been updated by nobu (Nobuyoshi Nakada).

Description updated

As it seems useful only when `condition` doesn't use the parameter `query`, it is questionable to me if it is generic enough to be a language feature.

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

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:92621] [Ruby trunk Feature#15829] Object#then_if(condition){}
       [not found] <redmine.issue-15829.20190505182618@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2019-05-11  7:13 ` [ruby-core:92620] " nobu
@ 2019-05-11 12:11 ` sawadatsuyoshi
  2019-07-29  6:58 ` [ruby-core:93967] [Ruby master " ko1
  2019-12-15  8:22 ` [ruby-core:96245] " masafumi.o1988
  8 siblings, 0 replies; 9+ messages in thread
From: sawadatsuyoshi @ 2019-05-11 12:11 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:93967] [Ruby master Feature#15829] Object#then_if(condition){}
       [not found] <redmine.issue-15829.20190505182618@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2019-05-11 12:11 ` [ruby-core:92621] " sawadatsuyoshi
@ 2019-07-29  6:58 ` ko1
  2019-12-15  8:22 ` [ruby-core:96245] " masafumi.o1988
  8 siblings, 0 replies; 9+ messages in thread
From: ko1 @ 2019-07-29  6:58 UTC (permalink / raw)
  To: ruby-core

Issue #15829 has been updated by ko1 (Koichi Sasada).

Status changed from Open to Rejected

It seems difficult to introduce.

if you are interest about this ticket yet, could you reopen it and file on our dev-meeting agenda?
https://bugs.ruby-lang.org/issues/15996

Thanks.

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

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [ruby-core:96245] [Ruby master Feature#15829] Object#then_if(condition){}
       [not found] <redmine.issue-15829.20190505182618@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2019-07-29  6:58 ` [ruby-core:93967] [Ruby master " ko1
@ 2019-12-15  8:22 ` masafumi.o1988
  8 siblings, 0 replies; 9+ messages in thread
From: masafumi.o1988 @ 2019-12-15  8:22 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2019-12-15  8:22 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [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 ` [ruby-core:96245] " masafumi.o1988

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).