aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-05-28 23:33:54 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-05-28 23:33:54 +0000
commit428d1f67dab05b84330c7fc66bff0202aee91bd8 (patch)
treea5f38dbd990030e29d6891d7a3b23fc37275e705
parentd8b67f7fe1ff281a7ba00bd187fe938a12bb5566 (diff)
downloadrails-428d1f67dab05b84330c7fc66bff0202aee91bd8.tar.gz
rails-428d1f67dab05b84330c7fc66bff0202aee91bd8.tar.bz2
rails-428d1f67dab05b84330c7fc66bff0202aee91bd8.zip
Multibyte strings respond_to the String methods they proxy so they can be duck-typed. Closes #6549.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6882 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activesupport/CHANGELOG4
-rw-r--r--activesupport/lib/active_support/multibyte/chars.rb6
-rw-r--r--activesupport/test/multibyte_chars_test.rb7
3 files changed, 16 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 81f78bb9a7..2999f75c7f 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Multibyte strings respond_to the String methods they proxy so they can be duck-typed. #6549 [Tuxie]
+
* Array#to_xml yields the builder just like Hash and ActiveRecord::Base. #8472 [seth]
* 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 @@
* Make String#chars slicing behaviour consistent with String. Closes #6387 [Manfred Stienstra]
-* Pull in latest multibye patch. Closes #6346 [Manfred Stienstra]
+* Pull in latest multibyte patch. Closes #6346 [Manfred Stienstra]
* 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]
diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb
index 901815092d..65114415eb 100644
--- a/activesupport/lib/active_support/multibyte/chars.rb
+++ b/activesupport/lib/active_support/multibyte/chars.rb
@@ -41,6 +41,12 @@ module ActiveSupport::Multibyte #:nodoc:
@string
end
+ # Make duck-typing with String possible
+ def respond_to?(method)
+ super || @string.respond_to?(method) || handler.respond_to?(method) ||
+ (method.to_s =~ /(.*)!/ && handler.respond_to?($1)) || false
+ end
+
# Create a new Chars instance.
def initialize(str)
@string = str.respond_to?(:string) ? str.string : str
diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb
index 63aff6eb99..d8766af3b8 100644
--- a/activesupport/test/multibyte_chars_test.rb
+++ b/activesupport/test/multibyte_chars_test.rb
@@ -151,6 +151,13 @@ class CharsTest < Test::Unit::TestCase
end
end
+ def test_duck_typing
+ assert_equal true, 'test'.chars.respond_to?(:strip)
+ assert_equal true, 'test'.chars.respond_to?(:normalize)
+ assert_equal true, 'test'.chars.respond_to?(:normalize!)
+ assert_equal false, 'test'.chars.respond_to?(:a_method_that_doesnt_exist)
+ end
+
protected
def with_kcode(kcode)