This project is archived and is in readonly mode.

#4717 ✓resolved
ronin-56076 (at lighthouseapp)

[PATCH] Fix ActiveSupport::Multibyte::Chars#slice for empty strings when starting offset is negative

Reported by ronin-56076 (at lighthouseapp) | May 28th, 2010 @ 07:22 AM

ActiveSupport::Multibyte::Chars#slice crashes for a negative starting offset if the string is empty.

Steps to reproduce:

$ script/console
Loading development environment (Rails 2.3.8)

rails(test)> 'test'.slice(-1, 1)
=> "t"

rails(test)> ''.slice(-1, 1)
=> nil

rails(test)> 'test'.mb_chars.slice(-1, 1)
=> #<ActiveSupport::Multibyte::Chars:0x103979830 @wrapped_string="t">

rails(test)> ''.mb_chars.slice(-1, 1)
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.pack
    from /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/multibyte/chars.rb:347:in `slice'
    from (irb):4

See the attached patch.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages