I have a nested hash in ruby like this
a = {
'a': 1,
'b': 2,
'c': {
'd': 3
}
}
=> {:a=>1, :b=>2, :c=>{:d=>3}}
and I set a.default = ''
How could I get the value of d if I use a string interpolation expression like:
puts "%{c['d']}" % a
I have unsuccessfully tried
puts "%{c}" % a
{:d=>3}
=> nil
puts "%{c['d']}" % a
=> nil
puts "%{c[:d]}" % a
=> nil
I would need some way to get the nested 3 in a['c']['d']. The two previous examples would suit me but they return empty string.
ps. If I don't use the a.default = '' I get the error
puts "%{c[:d]}" % a
KeyError: key{c[:d]} not found
from (pry):45:in `%'
**p.s: I'm using pry to run the code
I looked at the Ruby source code for
sprintfand this does not appear to be possible. The relevant section is reproduced below.