Issue #15563 has been updated by shevegen (Robert A. Heiler).


I have no particular pro or con against the feature itself as such; I myself do not use or need .dig so I
can not speak much about it. But I believe one problem with the proposal here is the name.

I think a name such as "dig_e" would be very, very rare to see in ruby. Of course I have no idea how
matz thinks about it, but I would recommend to you to also consider alternative names; or perhaps
let it handle just through arguments, whatever may seem to fit better.

Short names are sometimes really, really great, such as p and pp; but I think one overall concern may
be to not lose too much of the meaning. Off the top of my head, I can only think of FileUtils having 
odd/very short method names, and this is mostly because it sort of "simulates" how coreutils utilities
such as "mkdir -p" and similar work.

If you look at recent changes in ruby, you may notice the :exception key - :e would be shorter than
that too, but I think it may not be a primary goal at all times to be too overly succinct, so if that is
a valid reasoning then I think this may explain why :exception would be used, and no shorter
variant. A similar reasoning could apply to the case here - but again, ultimately you have to see what
matz thinks about it not how others may think about it. :)


----------------------------------------
Feature #15563: #dig that throws an exception if an key doesn't exist
https://bugs.ruby-lang.org/issues/15563#change-76515

* Author: 3limin4t0r (Johan Wentholt)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Ruby 2.3.0 introduced `#dig` for *Array*, *Hash* and *Struct*. Both *Array* and *Hash* have `#fetch` which does the same as `#[]`, but instead of returning the default value an exception is raised (unless a second argument or block is given). *Hash* also has `#fetch_values` which does the same as `#values_at`, raising an exception if an key is missing. For `#dig` there is no such option.

My proposal is to add a method which does the same as `#dig`, but instead of using the `#[]` accessor it uses `#fetch`.

This method would look something like this:

```Ruby
module DigWithException
  def dig_e(key, *others)
    value = fetch(key)
    return value if value.nil? || others.empty?

    if value.respond_to?(__method__, true)
      value.send(__method__, *others)
    else
      raise TypeError, "#{value.class} does not have ##{__method__} method"
    end
  end
end

Array.include(DigWithException)
Hash.include(DigWithException)
```

The exception raised is also taken from `#dig` (`[1].dig(0, 1) #=> TypeError: Integer does not have #dig method`). I personally have my issues with the name `#dig_e`, but I haven't found a better name yet.

There are also a few other things that I haven't thought out yet.

 1. Should this method be able to accept a block which, will be passed to the `#fetch` call and recursive `#dig_e` calls?  

    ```Ruby
    module DigWithException
      def dig_e(key, *others, &block)
        value = fetch(key, &block)
        return value if value.nil? || others.empty?

        if value.respond_to?(__method__, true)
          value.send(__method__, *others, &block)
        else
          raise TypeError, "#{value.class} does not have ##{__method__} method"
        end
      end
    end

    Array.include(DigWithException)
    Hash.include(DigWithException)
    ```

 2. I currently kept the code compatible with the `#dig` description.

    > Extracts the nested value specified by the sequence of *key* objects by calling `dig` at each step, returning `nil` if any intermediate step is `nil`.

    However, with this new version of the method one could consider dropping the *"returning `nil` if any intermediate step is `nil`"* part, since this would be the more strict version.

    ```Ruby
    module DigWithException
      def dig_e(key, *others)
        value = fetch(key)
        return value if others.empty?

        if value.respond_to?(__method__, true)
          value.send(__method__, *others)
        else
          raise TypeError, "#{value.class} does not have ##{__method__} method"
        end
      end
    end

    Array.include(DigWithException)
    Hash.include(DigWithException)
    ```

I'm curious to hear what you guys think about the idea as a whole, the method name and the two points described above.
 



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