Project

General

Profile

Bug #11618

Safe call syntax with aref or aset is

Added by yui-knk (Kaneko Yuichiro) about 4 years ago. Updated 5 months ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.0dev (2015-10-24 trunk 52128)
[ruby-dev:49312]

Description

This test pass

# 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 -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

Related issues

Related to Ruby master - Feature #11813: Extend safe navigation operator for [] and []= with syntax sugarRejectedActions

History

Updated by matz (Yukihiro Matsumoto) about 4 years ago

I am not going to add safe navigation for aref and aset.
Maybe you can use a.?[](x) (method call of an operator).

Matz.

#2

Updated by yui-knk (Kaneko Yuichiro) almost 4 years ago

  • Related to Feature #11813: Extend safe navigation operator for [] and []= with syntax sugar added
#3

Updated by jeremyevans0 (Jeremy Evans) 5 months ago

  • Status changed from Open to Rejected

Also available in: Atom PDF