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.6 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_HELO_NONE,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 D9E321F463 for ; Sun, 15 Dec 2019 08:22:46 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 7C108120A72; Sun, 15 Dec 2019 17:22:34 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id E7986120A71 for ; Sun, 15 Dec 2019 17:22:31 +0900 (JST) Received: by filter0168p3mdw1.sendgrid.net with SMTP id filter0168p3mdw1-25125-5DF5ED4D-13 2019-12-15 08:22:37.89557435 +0000 UTC m=+224158.622410799 Received: from herokuapp.com (unknown [184.72.70.224]) by ismtpd0098p1mdw1.sendgrid.net (SG) with ESMTP id eSqYNZdSS1-MvgioqmGhTg for ; Sun, 15 Dec 2019 08:22:37.728 +0000 (UTC) Date: Sun, 15 Dec 2019 08:22:37 +0000 (UTC) From: masafumi.o1988@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 71900 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15829 X-Redmine-Issue-Author: foonlyboy X-Redmine-Sender: okuramasafumi 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?GD8sp3VOc0XbLIYbGNvmfCikZu7=2FZy1HfV9bhiGCQA=2FLdrnE9FiglUEemywhUJ?= =?us-ascii?Q?Y3bD94lrKY0If24uhIjcZTGHeGoRBzXwdD6fsi+?= =?us-ascii?Q?LUcFfXogezz8+Qg1CjD278OXaV=2Fk9kbCS+pXkp4?= =?us-ascii?Q?Q8W5JwlXMn=2Fx6WROSBgDGHukLa+tHFBpuwvaMoo?= =?us-ascii?Q?dxis64DbQ0RP15yaUyYtGlh9i7qGcFe6lbg=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 96245 Subject: [ruby-core:96245] [Ruby master 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 okuramasafumi (Masafumi OKURA). 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. ---------------------------------------- Feature #15829: Object#then_if(condition){} https://bugs.ruby-lang.org/issues/15829#change-83137 * Author: foonlyboy (Eike Dierks) * Status: Rejected * 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: ```ruby 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: ```ruby 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/