From ab51b021856ccafb1024fae493a405d590142a07 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Wed, 11 Nov 2015 16:52:48 +0900 Subject: [PATCH] Not raise `KeyError` when an arg hash of `sprintf` has key whose value is `nil` --- sprintf.c | 2 +- test/ruby/test_sprintf.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/sprintf.c b/sprintf.c index 5c0f940..10a8aed 100644 --- a/sprintf.c +++ b/sprintf.c @@ -609,7 +609,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) len - 2 /* without parenthesis */, enc); nextvalue = rb_hash_aref(hash, sym); - if (NIL_P(nextvalue) && !FL_TEST(hash, HASH_PROC_DEFAULT)) { + if (NIL_P(nextvalue) && (rb_hash_has_key(hash, sym) == Qfalse) && !FL_TEST(hash, HASH_PROC_DEFAULT)) { rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start); } if (term == '}') goto format_s; diff --git a/test/ruby/test_sprintf.rb b/test/ruby/test_sprintf.rb index 97eb480..8af94a8 100644 --- a/test/ruby/test_sprintf.rb +++ b/test/ruby/test_sprintf.rb @@ -415,4 +415,9 @@ class TestSprintf < Test::Unit::TestCase assert_equal("hello world", "hello %{location}" % h) assert_equal("hello world", "hello %s" % h) end + + def test_named_with_nil + h = { key: nil, key2: "key2_val" } + assert_equal("key is , key2 is key2_val", "key is %{key}, key2 is %{key2}" % h) + end end -- 2.3.2 (Apple Git-55)