From 50fd1e85ea36769ca278d6eb3c84a5e36f80fac4 Mon Sep 17 00:00:00 2001 From: Henrik N Date: Thu, 25 Feb 2010 12:44:49 +0100 Subject: [PATCH] Fix literal "%%" in string interpolation without a hash. Solution by clemens, copied over from the i18n gem: http://github.com/svenfuchs/i18n/commit/bd740600683ca2aae78e4abf898f011627b623e0 --- .../core_ext/string/interpolation.rb | 9 +++++++-- activesupport/test/core_ext/string_ext_test.rb | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/string/interpolation.rb b/activesupport/lib/active_support/core_ext/string/interpolation.rb index 06d3505..a06d3fd 100644 --- a/activesupport/lib/active_support/core_ext/string/interpolation.rb +++ b/activesupport/lib/active_support/core_ext/string/interpolation.rb @@ -27,10 +27,15 @@ if RUBY_VERSION < '1.9' && !"".respond_to?(:interpolate_without_ruby_19_syntax) alias :interpolate_without_ruby_19_syntax :% # :nodoc: INTERPOLATION_PATTERN = Regexp.union( - /%%/, /%\{(\w+)\}/, # matches placeholders like "%{foo}" /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/ # matches placeholders like "%.d" ) + + INTERPOLATION_PATTERN_WITH_ESCAPE = Regexp.union( + /%%/, + INTERPOLATION_PATTERN + ) + # % uses self (i.e. the String) as a format specification and returns the # result of applying it to the given arguments. In other words it interpolates @@ -71,7 +76,7 @@ if RUBY_VERSION < '1.9' && !"".respond_to?(:interpolate_without_ruby_19_syntax) # # => "10, 43.3" def %(args) if args.kind_of?(Hash) - dup.gsub(INTERPOLATION_PATTERN) do |match| + dup.gsub(INTERPOLATION_PATTERN_WITH_ESCAPE) do |match| if match == '%%' '%' else diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index d8145d4..e89039e 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -319,7 +319,7 @@ class TestGetTextString < Test::Unit::TestCase end def test_sprintf_old_style - assert_equal("foo 1.000000", "%s %f" % ["foo", 1.0]) + assert_equal("foo 1.000000%", "%s %f%%" % ["foo", 1.0]) end def test_sprintf_mix_unformatted_and_formatted_named_placeholders -- 1.6.4.2