[#90865] [Ruby trunk Bug#15499] Breaking behavior on ruby 2.6: rb_thread_call_without_gvl doesn't invoke unblock_function when used on the main thread — apolcyn@...
Issue #15499 has been reported by apolcyn (alex polcyn).
3 messages
2019/01/03
[#90877] [Ruby trunk Bug#15499] Breaking behavior on ruby 2.6: rb_thread_call_without_gvl doesn't invoke unblock_function when used on the main thread — apolcyn@...
Issue #15499 has been updated by apolcyn (alex polcyn).
3 messages
2019/01/03
[#90895] Re: [ruby-alerts:11680] failure alert on trunk-mjit@silicon-docker (NG (r66707)) — Eric Wong <normalperson@...>
ko1c-failure@atdot.net wrote:
4 messages
2019/01/05
[#90896] Re: [ruby-alerts:11680] failure alert on trunk-mjit@silicon-docker (NG (r66707))
— Takashi Kokubun <takashikkbn@...>
2019/01/05
Thanks to explain that.
[#91200] [Ruby trunk Feature#15553] Addrinfo.getaddrinfo supports timeout — glass.saga@...
Issue #15553 has been reported by Glass_saga (Masaki Matsushita).
4 messages
2019/01/21
[#91289] Re: [Ruby trunk Feature#15553] Addrinfo.getaddrinfo supports timeout
— Eric Wong <normalperson@...>
2019/01/26
glass.saga@gmail.com wrote:
[ruby-core:90847] [Ruby trunk Feature#15492] Let #dig take a "default value" block like Hash#fetch does
From:
tyler@...
Date:
2019-01-01 23:14:21 UTC
List:
ruby-core #90847
Issue #15492 has been reported by TylerRick (Tyler Rick).
----------------------------------------
Feature #15492: Let #dig take a "default value" block like Hash#fetch does
https://bugs.ruby-lang.org/issues/15492
* Author: TylerRick (Tyler Rick)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
[fetch](https://ruby-doc.org/core-2.6/Hash.html#method-i-fetch) provides multiple ways to handle the case where a key can't be found:
> If the key can't be found, there are several options: With no other arguments, it will raise a KeyError exception; if `default` is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned.
~~~ ruby
{a: 'a'}.fetch(:d) { 'default' }
#=> "default"
{a: 'a'}.fetch(:d) {|key| key }
#=> :d
~~~
[dig](https://ruby-doc.org/core-2.6/Hash.html#method-i-dig) obviously can't let you specify a `default` value as a positional argument like `fetch` does, but couldn't it at least let you specify a default value by passing a block?
The fact that it currently just silently *ignores* the block that you pass to `dig` could be misleading, as one might assume (as I did at first) that it's going to return that in case any of the key lookups fail.
Current/desired behavior:
~~~ ruby
{a: {b: {c: 'c'}}}.dig(:a, :b, :d) {|key| key }
#=> nil
# wish it returned :d
{a: {b: {c: 'c'}}}.dig(:a, :b, :d) {|key| 'default' }
#=> nil
# wish it returned 'default'
~~~
There isn't currently a nice way to do this (that I can think of). You could mix `dig` and `fetch` (or chain a bunch of `fetch`s), but that loses the simple elegance of `dig` and looks awful:
~~~ ruby
object = {a: {b: {c: 'c'}}}
(object.dig(:a, :b) || {}).fetch(:d) { 'default' }
#=> "default"
~~~
Note, of course, that if we added default-block behavior to `dig`, its behavior would be slightly different: it would return the result of the default block if _any_ intermediate step were `nil`, not just if the _last_ lookup were nil.
~~~ ruby
object = {a: {b: {c: 'c'}}}
object.dig(:x, :y, :z) { 'default' }
#=> "default"
~~~
## Example use case
Sometimes you start out with a simple Hash but over time you may end up moving keys into sub-hashes.
You might have started out with something like
~~~ ruby
config.fetch(:something) { 'default' }
~~~
But after you move `:something` under `:settings`, you have to use `dig` instead of `fetch`:
~~~ ruby
config.dig(:settings, :something)
~~~
The problem is, how do you keep the default value now that it's in a sub-hash? Can't just use `|| 'default'` because the stored value might be `false`.
Currently I'm working around this with an explicit `nil?` check...
~~~ ruby
value = config.dig(:settings, :something)
value.nil? ? 'default' : value
~~~
(Even *that* may not be good enough if one wanted to distinguish between a `nil` value _stored_ in the object and the case where the key can't be found (a "cache miss"). Having a default block would let you distinguish between a missing key and a nil value stored at the key, in case that distinction were important...)
--
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>