Skip to content

Commit

Permalink
sprintf.c: hash default value
Browse files Browse the repository at this point in the history
* sprintf.c (rb_str_format): respect default value of a hash.  no
  longer raises KeyError unless the default value of the hash is
  nil.  [ruby-core:71354] [Bug ruby#11661]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52530 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Nov 11, 2015
1 parent 8d9e360 commit def4370
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Wed Nov 11 09:03:12 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>

* sprintf.c (rb_str_format): respect default value of a hash. no
longer raises KeyError unless the default value of the hash is
nil. [ruby-core:71354] [Bug #11661]

Tue Nov 10 20:35:12 2015 Tanaka Akira <akr@fsij.org>

* lib/open-uri.rb: Remove indicator for "frozen_string_literal: true".
Expand Down
10 changes: 5 additions & 5 deletions sprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,11 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
}
CHECKNAMEARG(start, len, enc);
get_hash(&hash, argc, argv);
sym = rb_check_symbol_cstr(start + 1,
len - 2 /* without parenthesis */,
enc);
if (sym != Qnil) nextvalue = rb_hash_lookup2(hash, sym, Qundef);
if (nextvalue == Qundef) {
sym = rb_cstr_intern(start + 1,
len - 2 /* without parenthesis */,
enc);
nextvalue = rb_hash_aref(hash, sym);
if (NIL_P(nextvalue) && !FL_TEST(hash, HASH_PROC_DEFAULT)) {
rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start);
}
if (term == '}') goto format_s;
Expand Down
6 changes: 6 additions & 0 deletions test/ruby/test_sprintf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,10 @@ def test_named_typed_enc
assert_equal(enc, e.message.encoding)
end
end

def test_named_default
h = Hash.new('world')
assert_equal("hello world", "hello %{location}" % h)
assert_equal("hello world", "hello %<location>s" % h)
end
end

0 comments on commit def4370

Please sign in to comment.