From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 6C3FD19C0049 for ; Fri, 23 Oct 2015 12:31:08 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 93B78B5D858 for ; Fri, 23 Oct 2015 12:58:22 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 7F4A2952408 for ; Fri, 23 Oct 2015 12:58:22 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 6C45F1204BD; Fri, 23 Oct 2015 12:58:18 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id 7C109120446 for ; Fri, 23 Oct 2015 12:58:14 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=/+fm9I2MYbbkP9PJXSQ/1/+Kklk=; b=T0hq7tCr/YCw6lkbki /c+0WnPNcPuxfwd3eS25BhsA+N1cFDyF2CAw9j/Kr8lulPN4tSYtFE3gvgkxBFUn BJBc2kKfThNRyuZPKUOvXc8V1+5IJsXV2FOQTvV7D//ri9YnsEoKJujvFj0UJ7ci vc2pf9MQCA79WC5JoxjqUCh7g= Received: by filter-458.sjc1.sendgrid.net with SMTP id filter-458.9560.5629B0517 2015-10-23 03:58:09.204279149 +0000 UTC Received: from herokuapp.com (ec2-54-161-115-172.compute-1.amazonaws.com [54.161.115.172]) by ismtpd0001p1iad1.sendgrid.net (SG) with ESMTP id D41v7p3-QLuw3xyw-6GTFg Fri, 23 Oct 2015 03:58:09.100 +0000 (UTC) Date: Fri, 23 Oct 2015 03:58:09 +0000 From: treznick@continuity.net To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Redmine-MailingListIntegration-Message-Ids: 45794 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11537 X-Redmine-Issue-Author: hsbt X-Redmine-Issue-Assignee: matz X-Redmine-Sender: treznick 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS4F7LquYodrobb8FmYzFG4TWLYSxorWfLWBdY GwZhS5qP3/p2M60DwfAxUI7yPaaA6+ZIFGNLPIbQjrrSB5XQYeOKspGfqBVtHHbVr3J4qwLQ9wDsBD 0liExNv8ReiJcmE= X-SendGrid-Contentd-ID: {"test_id":"1445572690"} X-ML-Name: ruby-core X-Mail-Count: 71169 Subject: [ruby-core:71169] [Ruby trunk - Feature #11537] Introduce "Safe navigation operator" 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: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #11537 has been updated by Tom Reznick. Thanks for the thoughtful replies guys! That definitely helps clarify the `.?` operator Matthew Kerwin wrote: > On 23/10/2015 2:46 AM, wrote: > > > > Issue #11537 has been updated by Jeremy Evans. > > > > > > Tom Reznick wrote: > > > Hi, > > > > > > I think we may have found some unexpected behavior with the `.?` > operator. > > > > > > If I call the following: > > > > > > s = Struct.new(:x) > > > o = s.new() > > > o.x #=> nil > > > o.x.nil? #=> true > > > o.x.?nil? #=> nil > > > o.x.kind_of?(NilClass) #=> true > > > o.x.?kind_of?(NilClass) #=> nil > > > o.x.methods.include?(:nil?) #=> true > > > > > > > > > While it's arguably a bit peculiar to try to check that `nil` is `nil`, > in a `nil`-safe way, `.?kind_of?(NilClass)` could reasonably return `true`. > > > > I think it's completely expected that `nil.?kind_of?(NilClass)` returns > `nil` and not `true`. The whole point of `.?` is to return `nil` without > calling the method if the receiver is `nil`. I'm not sure if `.?` is a > good idea syntax-wise, but if you are going to have it, it shouldn't have > special cases for specific methods. > > > > I agree, or put another way: if you're testing for nil in two ways, .? has > higher priority. That makes it a programmer issue, not a ruby one. ---------------------------------------- Feature #11537: Introduce "Safe navigation operator" https://bugs.ruby-lang.org/issues/11537#change-54543 * Author: Hiroshi SHIBATA * Status: Closed * Priority: Normal * Assignee: Yukihiro Matsumoto ---------------------------------------- I sometimes write following code with rails application: ```ruby u = User.find(id) if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large ... ``` or ```ruby # Use ActiveSupport if u.try!(:profile).try!(:thumbnails).try!(:large) ... ``` I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "`?.`" syntax. Ruby can't use "`?.`" operator. Can we use "`.?`" syntax. like this: ```ruby u = User.find(id) u.?profile.?thumbnails.?large ``` Matz. How do you think about this? -- https://bugs.ruby-lang.org/