[前][次][番号順一覧][スレッド一覧]

ruby-changes:40449

From: nobu <ko1@a...>
Date: Wed, 11 Nov 2015 09:03:34 +0900 (JST)
Subject: [ruby-changes:40449] nobu:r52530 (trunk): sprintf.c: hash default value

nobu	2015-11-11 09:03:15 +0900 (Wed, 11 Nov 2015)

  New Revision: 52530

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52530

  Log:
    sprintf.c: hash default value
    
    * 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]

  Modified files:
    trunk/ChangeLog
    trunk/sprintf.c
    trunk/test/ruby/test_sprintf.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52529)
+++ ChangeLog	(revision 52530)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Nov 11 09:03:12 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@f...>
 
 	* lib/open-uri.rb: Remove indicator for "frozen_string_literal: true".
Index: sprintf.c
===================================================================
--- sprintf.c	(revision 52529)
+++ sprintf.c	(revision 52530)
@@ -605,11 +605,11 @@ rb_str_format(int argc, const VALUE *arg https://github.com/ruby/ruby/blob/trunk/sprintf.c#L605
 		}
 		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;
Index: test/ruby/test_sprintf.rb
===================================================================
--- test/ruby/test_sprintf.rb	(revision 52529)
+++ test/ruby/test_sprintf.rb	(revision 52530)
@@ -409,4 +409,10 @@ class TestSprintf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_sprintf.rb#L409
       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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]