Project

General

Profile

Actions

Feature #15829

closed

Object#then_if(condition){}

Added by foonlyboy (Eike Dierks) over 5 years ago. Updated almost 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:92559]

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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0