From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.8 required=3.0 tests=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 1DE7A1F453 for ; Wed, 23 Jan 2019 09:27:50 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 85FF11218D1; Wed, 23 Jan 2019 18:27:47 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id BF8AD1218D1 for ; Wed, 23 Jan 2019 18:27:45 +0900 (JST) Received: by filter0025p3iad2.sendgrid.net with SMTP id filter0025p3iad2-21809-5C48338F-11 2019-01-23 09:27:43.354121871 +0000 UTC m=+735047.885255119 Received: from herokuapp.com (ec2-54-211-52-65.compute-1.amazonaws.com [54.211.52.65]) by ismtpd0015p1iad1.sendgrid.net (SG) with ESMTP id _xd_yS00TiugAQ0K-E-mUw for ; Wed, 23 Jan 2019 09:27:43.156 +0000 (UTC) Date: Wed, 23 Jan 2019 09:27:44 +0000 (UTC) From: nobu@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 66671 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15557 X-Redmine-Issue-Author: sawa X-Redmine-Sender: nobu X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS4q3zdzxz3XTrByCKQB3Jo585f3E5q0p5RUiX BaGWqAlyBaA3HrTIrzUsrjfdZKomdQDoQc/cDJQtwBv6fBFE2td6Fpu4m/qpJnARDzsZwabU66gu2F Ttv7HAzYs41fubi2VBeMlZn0CtmEhucEeKUV2BLUsaZcQ+c+iirlaajISQ== X-ML-Name: ruby-core X-Mail-Count: 91223 Subject: [ruby-core:91223] [Ruby trunk Feature#15557] A new class that stores a condition and the previous receiver X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #15557 has been updated by nobu (Nobuyoshi Nakada). What about: ```ruby foo = default_definition .when(->(foo) {some_condition(foo)}) {|foo| some_method(foo)} .when(->(foo) {another_condition(foo)}) {|foo| another_method(foo)} ``` ---------------------------------------- Feature #15557: A new class that stores a condition and the previous receiver https://bugs.ruby-lang.org/issues/15557#change-76463 * 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 have 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 way. ```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/