ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: Colin Kelley <colindkelley@gmail.com>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:71738] behavior of Array#dig when passed a non-numeric index?
Date: Sun, 29 Nov 2015 15:24:33 -0800	[thread overview]
Message-ID: <CALGDcFvbpS5JdJ97noSNXq5cs5m7UfJkhf2fWYdgYVzrg+Wtcg@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1184 bytes --]

Hello,

I am working on this gem to provide backward-compatibility of the new `dig`
method in `Hash` and `Array`.

    https://rubygems.org/gems/ruby_dig

(See the accompanying blog post for more info.
http://development.invoca.com/ruby_dig-gem-adds-hash-dig-and-array-dig-from-ruby-2-3-to-earlier-versions/
)

But I can't tell from the documentation or tests in the original commit


https://github.com/ruby/ruby/commit/29862685c0acf3a40c6b1f9e8780cbbd86cba658

what is supposed to happen if Array#dig is passed a non-numeric index. For
example:

    ['zero', 'one', 'two'].dig(:first)

It seems like the C code is just calling the `[]` operator, in which case
it would raise this exception:

    TypeError: no implicit conversion of Symbol into Integer
But it definitely seems like the spirit of `dig` that `nil` should be
returned if the expected structure isn't there.  I believe the most typical
use case for `dig` is to dig through a JSON hash and either find the result
we expected, or get `nil`.  Wouldn't it defeat the purpose of `dig` if we
had to wrap it in a `rescue`?

Can we clarify the desired behavior, then update the documentation and
tests to reflect that?

-Colin

[-- Attachment #2: Type: text/html, Size: 1937 bytes --]

                 reply	other threads:[~2015-11-29 22:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=CALGDcFvbpS5JdJ97noSNXq5cs5m7UfJkhf2fWYdgYVzrg+Wtcg@mail.gmail.com \
    --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).