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=-2.8 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_PASS shortcircuit=no autolearn=no 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 EBD611F45F for ; Sun, 5 May 2019 20:30:44 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 67036120A77; Mon, 6 May 2019 05:30:39 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 13E261209DA for ; Mon, 6 May 2019 05:30:36 +0900 (JST) Received: by filter0004p3iad2.sendgrid.net with SMTP id filter0004p3iad2-21895-5CCF47ED-34 2019-05-05 20:30:37.566186089 +0000 UTC m=+263080.152834894 Received: from herokuapp.com (unknown [35.172.227.62]) by ismtpd0050p1iad1.sendgrid.net (SG) with ESMTP id 6nw3U9bCRpm5lY8sa__45Q for ; Sun, 05 May 2019 20:30:37.485 +0000 (UTC) Date: Sun, 05 May 2019 20:30:37 +0000 (UTC) From: foonlyboy@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68047 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15829 X-Redmine-Issue-Author: foonlyboy X-Redmine-Sender: foonlyboy 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: =?us-ascii?Q?C1sY0KTFJLESsrpdDIZfILysIdYN9fBoi0I2HVzpA6BHzJHcY3uMKnx=2FmAP51j?= =?us-ascii?Q?NQ0bo4UpF8sBC7mWclS2gIQIhmSUWs7fvLmWQ3i?= =?us-ascii?Q?JvDixeH8Ybn7=2FPWy=2FTYF9GkTQ2H4LgEorgMP4Hc?= =?us-ascii?Q?cSBK5SihVXztXuF7f9ZJhZPymMpQYwMEon9ozgO?= =?us-ascii?Q?J5AkIFf+axTOzsIz=2FovC716d4bpl=2FKXb=2F4Q=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92564 Subject: [ruby-core:92564] [Ruby trunk Feature#15829] Object#then_if(condition){} 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 #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/