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

ruby-changes:16706

From: naruse <ko1@a...>
Date: Wed, 21 Jul 2010 15:16:23 +0900 (JST)
Subject: [ruby-changes:16706] Ruby:r28702 (trunk): * sprintf.c: add short documentation about named reference.

naruse	2010-07-21 15:16:07 +0900 (Wed, 21 Jul 2010)

  New Revision: 28702

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

  Log:
    * sprintf.c: add short documentation about named reference.
      [ruby-core:31294]

  Modified files:
    trunk/ChangeLog
    trunk/sprintf.c
    trunk/test/ruby/test_sprintf.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 28701)
+++ ChangeLog	(revision 28702)
@@ -1,3 +1,8 @@
+Wed Jul 21 15:15:02 2010  NARUSE, Yui  <naruse@r...>
+
+	* sprintf.c: add short documentation about named reference.
+	  [ruby-core:31294]
+
 Wed Jul 21 15:00:19 2010  Nobuyoshi Nakada  <nobu@r...>
 
 	* parse.y (local_push_gen): disable unused variable warnings in
Index: sprintf.c
===================================================================
--- sprintf.c	(revision 28701)
+++ sprintf.c	(revision 28702)
@@ -420,6 +420,15 @@
  *     sprintf("%1$*2$s %2$d", "hello", -8)       #=> "hello    -8"
  *     sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23)   #=> "+1.23: 1.23:1.23"
  *     sprintf("%u", -123)                        #=> "-123"
+ *
+ *  For more complex formatting, Ruby supports a reference by name.
+ *  %<name>s style uses format style, but ${name} style doesn't.
+ *
+ *  Exapmles:
+ *    sprintf("%<foo>d : %<bar>f" % { :foo => 1, :bar => 2 })
+ *      #=> 1 : 2.000000
+ *    sprintf("%d %{foo}" % { :foo => 'bar' })
+ *      # => "%d bar"
  */
 
 VALUE
Index: test/ruby/test_sprintf.rb
===================================================================
--- test/ruby/test_sprintf.rb	(revision 28701)
+++ test/ruby/test_sprintf.rb	(revision 28702)
@@ -294,5 +294,8 @@
     assert_equal("value", sprintf("%<key>s", :key => "value"))
     assert_raise(ArgumentError) {sprintf("%1$<key2>s", :key => "value")}
     assert_raise(ArgumentError) {sprintf("%<key><key2>s", :key => "value")}
+    assert_equal("value", sprintf("%{key}", :key => "value"))
+    assert_raise(ArgumentError) {sprintf("%1${key2}", :key => "value")}
+    assert_equal("value{key2}", sprintf("%{key}{key2}", :key => "value"))
   end
 end

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

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