aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGourav Tiwari <gouravtiwari21@gmail.com>2015-06-29 20:20:50 +0000
committerSean Griffin <sean@seantheprogrammer.com>2015-10-20 15:59:57 -0600
commit09b46c7dbea6ac2ac09ba4a41fae0bf628a775f3 (patch)
tree55f60c9529e7237ec1597e955b44d67e1d3b9f73
parent1193824994887fb37ca492d7c5eb1f381f2fc220 (diff)
downloadrails-09b46c7dbea6ac2ac09ba4a41fae0bf628a775f3.tar.gz
rails-09b46c7dbea6ac2ac09ba4a41fae0bf628a775f3.tar.bz2
rails-09b46c7dbea6ac2ac09ba4a41fae0bf628a775f3.zip
Fixed slice! behavior: return nil for out-of-bound parameters
-rw-r--r--activesupport/lib/active_support/multibyte/chars.rb3
-rw-r--r--activesupport/test/multibyte_chars_test.rb4
2 files changed, 6 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb
index f6a2e7e949..a76692ec4b 100644
--- a/activesupport/lib/active_support/multibyte/chars.rb
+++ b/activesupport/lib/active_support/multibyte/chars.rb
@@ -94,7 +94,8 @@ module ActiveSupport #:nodoc:
# string.mb_chars.slice!(0..3) # => #<ActiveSupport::Multibyte::Chars:0x00000002eb80a0 @wrapped_string="Welo">
# string # => 'me'
def slice!(*args)
- chars(@wrapped_string.slice!(*args))
+ string_sliced = @wrapped_string.slice!(*args)
+ string_sliced ? chars(string_sliced) : nil
end
# Reverses all characters in the string.
diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb
index e1c4b705f8..42829c6a8e 100644
--- a/activesupport/test/multibyte_chars_test.rb
+++ b/activesupport/test/multibyte_chars_test.rb
@@ -413,6 +413,10 @@ class MultibyteCharsUTF8BehaviourTest < ActiveSupport::TestCase
assert_equal 'にち', @chars.slice!(1..2)
end
+ def test_slice_bang_returns_nil_on_out_of_bound_arguments
+ assert_equal nil, @chars.mb_chars.slice!(9..10)
+ end
+
def test_slice_bang_removes_the_slice_from_the_receiver
chars = 'úüù'.mb_chars
chars.slice!(0,2)