summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-07-22 13:58:10 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-07-22 13:58:10 +0000
commita4f2d6fec47f5aecb67151ea53e5498289169448 (patch)
treea6c6e929aaf2fe236ba8dc47219371de094bf34f
parent7909d9d0c7bbc38f3893df1b6f66863bb1082ac8 (diff)
merge revision(s) 28702:
* sprintf.c: add short documentation about named reference. [ruby-core:31294] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_2@28722 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--sprintf.c9
-rw-r--r--test/ruby/test_sprintf.rb3
3 files changed, 17 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index b7779d9e53..b9f51ed13c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Thu Jul 22 22:38:32 2010 NARUSE, Yui <naruse@ruby-lang.org>
+
+ * sprintf.c: add short documentation about named reference.
+ [ruby-core:31294]
+
Thu Jul 22 22:14:59 2010 NARUSE, Yui <naruse@ruby-lang.org>
* regexec.c (onig_search): don't skip non-ANYCHARs when
diff --git a/sprintf.c b/sprintf.c
index 8a4662bcfb..bf0d3d6fcf 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -420,6 +420,15 @@ get_hash(volatile VALUE *hash, int argc, const VALUE *argv)
* 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
diff --git a/test/ruby/test_sprintf.rb b/test/ruby/test_sprintf.rb
index 05bccde066..96a1b62bb7 100644
--- a/test/ruby/test_sprintf.rb
+++ b/test/ruby/test_sprintf.rb
@@ -294,5 +294,8 @@ class TestSprintf < Test::Unit::TestCase
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