Issue #13645 has been updated by osyo (manga osyo).


hi. I would like to use `hash&.[key]` (or `hash&[key]` ) in following cases as below.

```ruby
class X
  def initialize
    @hash = { a: 1, b: 2, c: 3 }
  end

  def [](key)
    @hash[key]
  end
end

def get(key)
  x = Symbol === key && X.new || nil
  # I want to use x&.[key] (or x&[key])
  x&.[](key)
end

p get(:a)     #=> 1
p get("b")    # => nil
```


Matz writes

> Use #dig for referencing the value.
> For updating, show us use cases.
> https://bugs.ruby-lang.org/issues/11813#note-6

However, YOU can not use `x&.dig(key)` for a class that Mr. Matz has previously proposed for which #dig is not defined.



----------------------------------------
Feature #13645: Syntactic sugar for indexing when using the safe navigation operator
https://bugs.ruby-lang.org/issues/13645#change-77648

* Author: ndn (Nikola Nenkov)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
# Proposal

While it works and makes sense, this is a bit cumbersome:

```ruby
hash&.[](:key)
```

Ideally, we could use something like:

```ruby
hash&.[:key]
```



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>