From: ruby-core@... Date: 2017-08-04T04:59:35+00:00 Subject: [ruby-core:82237] [Ruby trunk Bug#13781] Should the safe navigation operator invoke `nil?` Issue #13781 has been updated by marcandre (Marc-Andre Lafortune). Short answer is "no". Longer answer is: - is there an actual use case? I very much doubt there is one - BasicObject does not respond to `nil?`, so the safe operator would not work in that case? - other Ruby conditional (like `if foo` or `foo ? bar : baz`) do not call `nil?` or `==(nil)`, they simply do a straight comparison with `nil` and `false`. That's the way it should be for the safe navigation operator too. ---------------------------------------- Bug #13781: Should the safe navigation operator invoke `nil?` https://bugs.ruby-lang.org/issues/13781#change-66017 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: 2.4.1 * Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN ---------------------------------------- In the following example: ~~~ class Later < BasicObject def initialize(&block) raise ::ArgumentError, "Block required" unless block if block.arity > 0 raise ::ArgumentError, "Cannot store a promise that requires an argument" end @block = block end def __resolve__ @result ||= @block.call end def nil? __resolve__.nil? end def respond_to?(name) __resolve__.respond_to?(name) end def method_missing(name, *args, &block) __resolve__.__send__(name, *args, &block) end end Person = Struct.new(:name, :age) person = Later.new do nil # Person.new("Froob", 200) end puts person.nil? puts person&.name ~~~ The code fails because person is a proxy object. If safe navigation operator invoked `nil?` it should work. But, it's not clear exactly how the implementation should behave, or whether it's possible to implement this style of proxy. -- https://bugs.ruby-lang.org/ Unsubscribe: