[#108552] [Ruby master Bug#18782] Race conditions in autoload when loading the same feature with multiple threads. — "ioquatix (Samuel Williams)" <noreply@...>
Issue #18782 has been reported by ioquatix (Samuel Williams).
11 messages
2022/05/14
[ruby-core:108687] [Ruby master Feature#14602] Version of dig that raises error if a key is not present
From:
"ufuk (Ufuk Kayserilioglu)" <noreply@...>
Date:
2022-05-25 00:04:38 UTC
List:
ruby-core #108687
Issue #14602 has been updated by ufuk (Ufuk Kayserilioglu).
My humble suggestion would be `Hash#retrieve`. The operation tries to retrieve a value in the depths of a hash and if it comes back empty handed, that is an error.
```ruby
hash = {
name: {
first: "Ariel",
last: "Caplan"
}
}
hash.dig(:name, :first) # => Ariel
hash.dig(:name, :middle) # => nil
hash.retrieve(:name, :first) # => Ariel
hash.retrieve(:name, :middle) # => KeyError (key not found: :middle)
```
The word "retrieve" is [a synonym for "fetch"](https://www.thesaurus.com/browse/fetch) in English, and has deep roots in CS terminology related to "data retrieval" and similar.
----------------------------------------
Feature #14602: Version of dig that raises error if a key is not present
https://bugs.ruby-lang.org/issues/14602#change-97728
* Author: amcaplan (Ariel Caplan)
* Status: Open
* Priority: Normal
----------------------------------------
Currently, if I have a hash like this:
~~~ ruby
{
:name => {
:first => "Ariel",
:last => "Caplan"
}
}
~~~
and I want to navigate confidently and raise a KeyError if something is missing, I can do:
~~~ ruby
hash.fetch(:name).fetch(:first)
~~~
Unfortunately, the length of the name, combined with the need to repeat the method name every time, means most programmers are more likely to do this:
~~~ ruby
hash[:name][:first]
~~~
which leads to many unexpected errors.
The Hash#dig method made it easy to access methods safely from a nested hash; I'd like to have something similar for access without error protection, and I'd think the most natural name would be Hash#dig!. It would work like this:
~~~ ruby
hash = {
:name => {
:first => "Ariel",
:last => "Caplan"
}
}
hash.dig!(:name, :first) # => Ariel
hash.dig!(:name, :middle) # raises KeyError (key not found: :middle)
hash.dig!(:name, :first, :foo) # raises TypeError (String does not have #dig! method)
~~~
--
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>