ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: manga.osyo@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92305] [Ruby trunk Feature#13645] Syntactic sugar for indexing when using the safe navigation operator
Date: Tue, 16 Apr 2019 15:01:14 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77648.20190416150051.09efbd4f3b7920d6@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-13645.20170609073704@ruby-lang.org

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/

      parent reply	other threads:[~2019-04-16 15:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-13645.20170609073704@ruby-lang.org>
2017-06-09  7:37 ` [ruby-core:81629] [Ruby trunk Feature#13645] Syntactic sugar for indexing when using the safe navigation operator ndnenkov
2017-06-09  9:52 ` [ruby-core:81634] " shevegen
2017-06-09 10:13 ` [ruby-core:81636] " sawadatsuyoshi
2019-04-16 15:01 ` manga.osyo [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-77648.20190416150051.09efbd4f3b7920d6@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).