diff options
author | Norman Clarke <norman@njclarke.com> | 2010-07-08 17:42:14 -0300 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-07-08 22:59:24 +0200 |
commit | 6f83a57ac7bf77565380b26b506972cfe751b717 (patch) | |
tree | ed2b4779d2c1d4c0895c060fd265c8da3e7cb22a /activesupport/lib/active_support/multibyte | |
parent | 01629d180468049d17a8be6900e27a4f0d2b18c4 (diff) | |
download | rails-6f83a57ac7bf77565380b26b506972cfe751b717.tar.gz rails-6f83a57ac7bf77565380b26b506972cfe751b717.tar.bz2 rails-6f83a57ac7bf77565380b26b506972cfe751b717.zip |
Improve bang method defs, make slice! operate in-place. [#5028 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activesupport/lib/active_support/multibyte')
-rw-r--r-- | activesupport/lib/active_support/multibyte/chars.rb | 26 |
1 files changed, 7 insertions, 19 deletions
diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 8823e4a5ed..51c2a0edac 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -325,18 +325,6 @@ module ActiveSupport #:nodoc: end alias_method :[], :slice - # Like <tt>String#slice!</tt>, except instead of byte offsets you specify character offsets. - # - # Example: - # s = 'こんにちは' - # s.mb_chars.slice!(2..3).to_s #=> "にち" - # s #=> "こんは" - def slice!(*args) - slice = self[*args] - self[*args] = '' - slice - end - # Limit the byte size of the string to a number of bytes without breaking characters. Usable # when the storage for a string is limited for some reason. # @@ -425,14 +413,14 @@ module ActiveSupport #:nodoc: chars(Unicode.tidy_bytes(@wrapped_string, force)) end - %w(lstrip rstrip strip reverse upcase downcase tidy_bytes capitalize).each do |method| - define_method("#{method}!") do |*args| - unless args.nil? - @wrapped_string = send(method, *args).to_s - else - @wrapped_string = send(method).to_s + %w(capitalize downcase lstrip reverse rstrip slice strip tidy_bytes upcase).each do |method| + # Only define a corresponding bang method for methods defined in the proxy; On 1.9 the proxy will + # exclude lstrip!, rstrip! and strip! because they are already work as expected on multibyte strings. + if public_method_defined?(method) + define_method("#{method}!") do |*args| + @wrapped_string = send(args.nil? ? method : method, *args).to_s + self end - self end end |