From: sawadatsuyoshi@... Date: 2019-01-23T09:15:56+00:00 Subject: [ruby-core:91220] [Ruby trunk Feature#15557] A new class that stores a condition and the previous receiver Issue #15557 has been reported by sawa (Tsuyoshi Sawada). ---------------------------------------- Feature #15557: A new class that stores a condition and the previous receiver https://bugs.ruby-lang.org/issues/15557 * Author: sawa (Tsuyoshi Sawada) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I often see code like this: ```ruby foo = default_definition foo = some_method(foo) if some_condition(foo) foo = another_method(foo) if another_condition(foo) ... ``` It would be nice if we can write this as a method chain. Since we now how the method `then`, I thought it would be a nice fit to introduce a method called `when`, such that putting it right in front of `then` would execute the `then` method as ordinarily only when the condition is satisfied, and returns the previous receiver otherwise so that the code above can be rewritten as: ```ruby foo = default_definition .when{|foo| some_condition(foo)} .then{|foo| some_method(foo)} .when{|foo| another_condition(foo)} .then{|foo| another_method(foo)} ``` This proposal is also a generalization of what I intended to cover by https://bugs.ruby-lang.org/issues/13807. That is, ```ruby a.some_condition ? a : b ``` would rewritten as: ```ruby a.when(&:some_condition).then{b} ``` The proposal can be implemented by introducing a class called `Condition`, which stores a condition and the previous receiver, and works with `then` in a particular. ```ruby class Object def when Condition.new(self, yield(self)) end end class Condition def initialize default, condition @default, @condition = default, condition end def then @condition ? yield(@default) : @default end end ``` -- https://bugs.ruby-lang.org/ Unsubscribe: