Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify String#mb_chars and fix documentation. #8261

Merged
merged 2 commits into from Nov 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##

* No longer proxy ActiveSupport::Multibyte#class. *Steve Klabnik*

* Deprecate `ActiveSupport::TestCase#pending` method, use `skip` from MiniTest instead. *Carlos Antonio da Silva*

* `XmlMini.with_backend` now may be safely used with threads:
Expand Down
11 changes: 2 additions & 9 deletions activesupport/lib/active_support/core_ext/string/multibyte.rb
Expand Up @@ -6,7 +6,7 @@ class String
#
# +mb_chars+ is a multibyte safe proxy for string methods.
#
# In Ruby 1.8 and older it creates and returns an instance of the ActiveSupport::Multibyte::Chars class which
# It creates and returns an instance of the ActiveSupport::Multibyte::Chars class which
# encapsulates the original string. A Unicode safe version of all the String methods are defined on this proxy
# class. If the proxy class doesn't respond to a certain method, it's forwarded to the encapsulated string.
#
Expand All @@ -17,9 +17,6 @@ class String
# name.mb_chars.reverse.to_s # => "rellüM sualC"
# name.mb_chars.length # => 12
#
# In Ruby 1.9 and newer +mb_chars+ returns +self+ because String is (mostly) encoding aware. This means that
# it becomes easy to run one version of your code on multiple Ruby versions.
#
# == Method chaining
#
# All the methods on the Chars proxy which normally return a string will return a Chars object. This allows
Expand All @@ -36,11 +33,7 @@ class String
# For more information about the methods defined on the Chars proxy see ActiveSupport::Multibyte::Chars. For
# information about how to change the default Multibyte behavior see ActiveSupport::Multibyte.
def mb_chars
if ActiveSupport::Multibyte.proxy_class.consumes?(self)
ActiveSupport::Multibyte.proxy_class.new(self)
else
self
end
ActiveSupport::Multibyte.proxy_class.new(self)
end

def is_utf8?
Expand Down
5 changes: 4 additions & 1 deletion activesupport/test/multibyte_chars_test.rb
Expand Up @@ -47,7 +47,7 @@ def test_forwarded_bang_method_calls_should_return_nil_when_result_is_nil
end

def test_methods_are_forwarded_to_wrapped_string_for_byte_strings
assert_equal BYTE_STRING.class, BYTE_STRING.mb_chars.class
assert_equal BYTE_STRING.length, BYTE_STRING.mb_chars.length
end

def test_forwarded_method_with_non_string_result_should_be_returned_vertabim
Expand Down Expand Up @@ -673,6 +673,9 @@ def test_tidy_bytes_should_forcibly_tidy_bytes_if_specified
assert_equal "𥤤", chars(byte_string).tidy_bytes(true)
end

def test_class_is_not_forwarded
assert_equal BYTE_STRING.dup.mb_chars.class, ActiveSupport::Multibyte::Chars
end

private

Expand Down