Skip to content
This repository
Browse code

Multibyte strings respond_to the String methods they proxy so they ca…

…n be duck-typed. Closes #6549.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6882 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 428d1f67dab05b84330c7fc66bff0202aee91bd8 1 parent d8b67f7
Jeremy Kemper jeremy authored
4 activesupport/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* Multibyte strings respond_to the String methods they proxy so they can be duck-typed. #6549 [Tuxie]
  4 +
3 5 * Array#to_xml yields the builder just like Hash and ActiveRecord::Base. #8472 [seth]
4 6
5 7 * Date, Time, and DateTime support formatting blocks in addition to strftime strings. Introduce :long_ordinal format, e.g. "February 21st, 2005". #8191 [Coda Hale]
@@ -146,7 +148,7 @@
146 148
147 149 * Make String#chars slicing behaviour consistent with String. Closes #6387 [Manfred Stienstra]
148 150
149   -* Pull in latest multibye patch. Closes #6346 [Manfred Stienstra]
  151 +* Pull in latest multibyte patch. Closes #6346 [Manfred Stienstra]
150 152
151 153 * Add ActiveSupport::Multibyte. Provides String#chars which lets you deal with strings as a sequence of chars, not of bytes. Closes #6242 [Julian Tarkhanov, Manfred Stienstra, Thijs van der Vossen & Jan Behrens]
152 154
6 activesupport/lib/active_support/multibyte/chars.rb
@@ -41,6 +41,12 @@ def to_str
41 41 @string
42 42 end
43 43
  44 + # Make duck-typing with String possible
  45 + def respond_to?(method)
  46 + super || @string.respond_to?(method) || handler.respond_to?(method) ||
  47 + (method.to_s =~ /(.*)!/ && handler.respond_to?($1)) || false
  48 + end
  49 +
44 50 # Create a new Chars instance.
45 51 def initialize(str)
46 52 @string = str.respond_to?(:string) ? str.string : str
7 activesupport/test/multibyte_chars_test.rb
@@ -151,6 +151,13 @@ def test_resilience
151 151 end
152 152 end
153 153
  154 + def test_duck_typing
  155 + assert_equal true, 'test'.chars.respond_to?(:strip)
  156 + assert_equal true, 'test'.chars.respond_to?(:normalize)
  157 + assert_equal true, 'test'.chars.respond_to?(:normalize!)
  158 + assert_equal false, 'test'.chars.respond_to?(:a_method_that_doesnt_exist)
  159 + end
  160 +
154 161 protected
155 162
156 163 def with_kcode(kcode)

0 comments on commit 428d1f6

Please sign in to comment.
Something went wrong with that request. Please try again.