Feature #3450

Format Strings with Named Arguments & Hash#default

Added by Run Paint Run Run almost 4 years ago. Updated over 1 year ago.

[ruby-core:30803]
Status:Assigned
Priority:Low
Assignee:Nobuyoshi Nakada
Category:core
Target version:next minor

Description

=begin
Is it intentional that the, undocumented, feature #442 (named arguments in format strings) ignores the Hash's default value?

hash = {greeting: 'Hello'}.tap{|h| h.default = 'World'}
'%{greeting}, %{greeted}' % hash
#=> KeyError: key{greeted} not found

It's quite common for templates to use default values. In interface localisation, untranslated strings may need to be substituted for the equivalent in the native language, or replaced with a generic string in the target language. When building reports with format strings, a default value of an empty string allows unexpanded format sequences to be non-fatal. In both cases, a default proc would provide even further flexibility.

Another perspective is that this behavior unnecessarily breaks the abstraction that default values are just like any other.
=end

3450.patch Magnifier (578 Bytes) Magnus Holm, 06/18/2010 07:07 PM

History

#1 Updated by Magnus Holm almost 4 years ago

=begin
I believe this patch should fix it, but this also has the side effect that KeyError will never be raised (since it will always use the default value, which is nil be default). There's probably some more code which can be removed if we apply the patch, but it seems to me that this is intentional behaviour though…
=end

#2 Updated by Hiroshi Nakamura about 2 years ago

  • Description updated (diff)
  • Assignee set to Nobuyoshi Nakada

#3 Updated by Shyouhei Urabe about 2 years ago

  • Status changed from Open to Assigned

#4 Updated by Yusuke Endoh over 1 year ago

  • Target version changed from 2.0.0 to next minor

#5 Updated by Nobuyoshi Nakada over 1 year ago

I think this is a trade-off.
Silently ignoring undefined keys may not preferable for somebody.
I don't think differentiating simple default value and default proc is good idea.

Also available in: Atom PDF