Now that we have Hash#dig
and Array#dig
coming for Ruby 2.3, I think that this proposal of mine from the past makes more sense.
Regarding Matz' point (2), this proposal should parallel the feature of dig
. It doesn't add extra cognitive load to us beyond what we would be knowing about dig
in the coming version of Ruby.
Regarding Matz' point (3), string format (%{}
) cannot be replaced by string interpolation (#{}
) when we want a static string that is to be used as a template, to which values would be inserted later to output a page. Interpolation requires dynamic evaluation of the string each time.
Regarding Matz' point (1), instead of using the period as the delimiter, we can use a comma (followed by optional space characters), which would resemble how dig
is called.
"Author: %{author, name} (%{author, affiliation}), %{date}" % {author: {name: "Ruby Taro", affiliation: "Ruby co."}, date: "2014, 8, 14"}
#=> "Author: Ruby Taro (Ruby co.), 2014, 8, 14"
This requires a key with a comma inside %{}
not to be interpreted as a single symbol but as a sequence of symbols separated by the comma (followed by space characters). Since it is rare that a comma is used in a symbol, I don't think it will affect existing code.