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-Status: No, score=-2.6 required=3.0 tests=AWL,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,UNPARSEABLE_RELAY 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 2FCA81F66F for ; Tue, 17 Nov 2020 12:49:10 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id CE4A2120A5A; Tue, 17 Nov 2020 21:48:26 +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 77E0A120A59 for ; Tue, 17 Nov 2020 21:48:24 +0900 (JST) Received: by filterdrecv-p3iad2-5dc87598f5-tptwj with SMTP id filterdrecv-p3iad2-5dc87598f5-tptwj-18-5FB3C6BF-2A 2020-11-17 12:49:03.297281511 +0000 UTC m=+58701.816369270 Received: from herokuapp.com (unknown) by ismtpd0066p1mdw1.sendgrid.net (SG) with ESMTP id Z1IGZSUdTk-CVwhgIzzKQQ for ; Tue, 17 Nov 2020 12:49:03.200 +0000 (UTC) Date: Tue, 17 Nov 2020 12:49:03 +0000 (UTC) From: zverok.offline@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 76771 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17330 X-Redmine-Issue-Author: zverok X-Redmine-Sender: zverok 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?3be0g8093pjUjT94eiCA64csFDBI=2FmHQTWm54P5gda4pw3gufs4VjQKWfQEa+h?= =?us-ascii?Q?MkSuYsuqCYOiwcB9qPamSSzAgjIJx96gNO8IgFh?= =?us-ascii?Q?shKfDWUMCwrGvSWd8vRw1DDY4SPQqPTsUNDpzDb?= =?us-ascii?Q?G53uK9b1FPID4owbv7pvTN+ORwStKogbVsOrMKa?= =?us-ascii?Q?7Bk=2FfTUywJ0Lq8JAX3LvILkrXFeKBIAq1DAv7x=2F?= =?us-ascii?Q?eVlHUpPCv=2Fea8jpUY=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 100897 Subject: [ruby-core:100897] [Ruby master Feature#17330] Object#non 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 #17330 has been reported by zverok (Victor Shepelev). ---------------------------------------- Feature #17330: Object#non https://bugs.ruby-lang.org/issues/17330 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal ---------------------------------------- (As always "with core" method proposals, I don't expect quick success, but hope for a fruitful discussion) **Reasons:** Ruby always tried to be very chainability-friendly. Recently, with introduction of `.then` and `=>`, even more so. But one pattern that frequently emerges and doesn't have good idiomatic expression: calculate something, and if it is not a "good" value, return `nil` (or provide default value with `||`). There are currently two partial solutions: 1. `nonzero?` in Ruby core (frequently mocked for "inadequate" behavior, as it is looking like predicate method, but instead of `true`/`false` returns an original value or `nil`) 2. ActiveSupport `Object#presence`, which also returns an original value or `nil` if it is not "present" (e.g. `nil` or `empty?` in AS-speak) Both of them prove themselves quite useful in some domains, but they are targeting only those particular domains, look unlike each other, and can be confusing. **Proposal:** Method `Object#non` (or `Kernel#non`), which receives a block, calls it with receiver and returns `nil` (if block matched) or receiver otherwise. Prototype implementation: ```ruby class Object def non self unless yield(self) end end ``` Usage examples: ``` # 1. With number: limit = calculate.some.limit limit.zero? ? DEFAULT_LIMIT : limit # or, with nonzero? calculate.some.limit.nonzero? || DEFAULT_LIMIT # with non: calculate.some.limit.non(&:zero?) || DEFAULT_LIMIT # ^ Note here, how, unlike `nonzero?`, we see predicate-y ?, but it is INSIDE the `non()` and less confusing # 2. With string: name = params[:name] if params[:name] && !params[:name].empty? # or, with ActiveSupport: name = params[:name].presence # with non: name = params[:name]&.non(&:empty?) # 3. More complicated example action = payload.dig('action', 'type') return if PROHIBITED_ACTIONS.include?(action) send("do_#{action}") # with non & then: payload.dig('action', 'type') .non { |action| PROHIBITED_ACTIONS.include?(action) } &.then { |action| send("do_#{action}") } ``` **Possible extensions of the idea** It is quite tempting to define the symmetric method named -- as we already have `Object#then` -- `Object#when`: ```ruby some.long.calculation.when { |val| val < 10 } # returns nil if value >= 10 # or even... with support for === some.long.calculation.when(..10)&.then { continue to do something } ``` ...but I am afraid I've overstayed my welcome :) -- https://bugs.ruby-lang.org/