aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
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