From: matz@... Date: 2015-10-24T15:17:13+00:00 Subject: [ruby-dev:49313] [Ruby trunk - Bug #11618] Safe call syntax with aref or aset is Issue #11618 has been updated by Yukihiro Matsumoto. I am not going to add safe navigation for aref and aset. Maybe you can use `a.?[](x)` (method call of an operator). Matz. ---------------------------------------- Bug #11618: Safe call syntax with aref or aset is https://bugs.ruby-lang.org/issues/11618#change-54559 * Author: Kaneko Yuichiro * Status: Open * Priority: Normal * Assignee: * ruby -v: ruby 2.3.0dev (2015-10-24 trunk 52128) * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN ---------------------------------------- This test pass ```ruby # ruby -v: ruby 2.3.0dev (2015-10-24 trunk 52128) [x86_64-darwin14] # test/ruby/test_call.rb def test_safe_call_aref_aset s = Struct.new(:x, :y) o = s.new({a: 10}) assert_equal(10, o.x.?send(:[], :a)) o.x.?send(:[]=, :a, 11) assert_equal(11, o.x.?send(:[], :a)) assert_nil(o.y.?send(:[], :a)) o.y.?send(:[]=, :a, 11) assert_nil(o.y.?send(:[], :a)) end ``` This is invalid syntax ```ruby # ruby -v: ruby 2.3.0dev (2015-10-24 trunk 52128) [x86_64-darwin14] # test/ruby/test_call.rb def test_safe_call_aref_aset s = Struct.new(:x, :y) o = s.new({a: 10}) assert_equal(10, o.x.?[:a]) o.x.?[:a] = 11 assert_equal(11, o.x.?[:a]) assert_nil(o.y.?[:a]) o.y.?[:a] = 11 assert_nil(o.y.?[:a]) end ``` ``` ./test/runner.rb: ruby/ruby/test/ruby/test_call.rb:60: syntax error, unexpected '[', expecting '(' (SyntaxError) assert_equal(10, o.x.?[:a]) ^ ruby/ruby/test/ruby/test_call.rb:61: syntax error, unexpected '[', expecting '(' o.x.?[:a] = 11 ^ ruby/ruby/test/ruby/test_call.rb:62: syntax error, unexpected '[', expecting '(' assert_equal(11, o.x.?[:a]) ^ ruby/ruby/test/ruby/test_call.rb:64: syntax error, unexpected '[', expecting '(' assert_nil(o.y.?[:a]) ^ ruby/ruby/test/ruby/test_call.rb:65: syntax error, unexpected '[', expecting '(' o.y.?[:a] = 11 ^ ruby/ruby/test/ruby/test_call.rb:66: syntax error, unexpected '[', expecting '(' assert_nil(o.y.?[:a]) ^ make: *** [yes-test-all] Error 1 ``` -- https://bugs.ruby-lang.org/