Feature #15829
closedObject#then_if(condition){}
Description
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.
Updated by foonlyboy (Eike Dierks) over 5 years ago
class Object
def then_if(condition, &block)
if condition
self.then(&block)
else
self
end
end
end
Updated by foonlyboy (Eike Dierks) over 5 years ago
A (somehow contrived) example:
[1,2,3,4,5].map{|n| n.then_if(n.even?){'even'}}
=> [1, "even", 3, "even", 5]
Updated by foonlyboy (Eike Dierks) over 5 years ago
A more complex example:
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 fromflag.nil?
-
then_if(seller)
only applies if aseller
was given -
then_if(! flag)
negates thequery
This brings the code down from 2x3=6 variants,
to only two code blocks (the second one being trivial in this case)
Updated by sawa (Tsuyoshi Sawada) over 5 years ago
Related to https://bugs.ruby-lang.org/issues/15557.
Updated by nobu (Nobuyoshi Nakada) over 5 years ago
- Description updated (diff)
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.
Updated by sawa (Tsuyoshi Sawada) over 5 years ago
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 #15557, where the condition is proposed to be given as a block (or a proc, following a suggestion by nobu).
Updated by ko1 (Koichi Sasada) over 5 years ago
- 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.
Updated by okuramasafumi (Masafumi OKURA) almost 5 years ago
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.