aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorGeorge Ogata <george.ogata@gmail.com>2008-12-30 14:16:15 +0100
committerPratik Naik <pratiknaik@gmail.com>2009-03-07 22:41:11 +0000
commit74387884819e8b6e16c3f67388610b8d1192cb4d (patch)
tree4bb0d6e6457ef628737f0674b516bd8071309ae7 /activesupport/lib/active_support
parentea8488caef77cb8cf2031d344e74981ab6ea0e57 (diff)
downloadrails-74387884819e8b6e16c3f67388610b8d1192cb4d.tar.gz
rails-74387884819e8b6e16c3f67388610b8d1192cb4d.tar.bz2
rails-74387884819e8b6e16c3f67388610b8d1192cb4d.zip
Make Chars#slice! behave more like String#slice! [#1243 state:resolved]
- Chars#slice! now returns the slice instead of itself - Chars#slice! now removes the slice from itself Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/multibyte/chars.rb16
1 files changed, 14 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb
index 62b6d798ef..60f082bcc1 100644
--- a/activesupport/lib/active_support/multibyte/chars.rb
+++ b/activesupport/lib/active_support/multibyte/chars.rb
@@ -344,7 +344,19 @@ module ActiveSupport #:nodoc:
end
alias_method :[], :slice
- # Converts first character in the string to Unicode value
+ # 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
+
+ # Returns the codepoint of the first character in the string.
#
# Example:
# 'こんにちは'.mb_chars.ord #=> 12371
@@ -432,7 +444,7 @@ module ActiveSupport #:nodoc:
chars(self.class.tidy_bytes(@wrapped_string))
end
- %w(lstrip rstrip strip reverse upcase downcase slice tidy_bytes capitalize).each do |method|
+ %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