Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#4717

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.

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

State: resolved
Milestone: none
Assigned to: José Valim José Valim
Importance: none

Activity

  1. ronin-56076 (at lighthouseapp)
    ronin-56076 (at lighthouseapp)
    • Tag changed from patch to activesupport, chars, multibyte, patch

    May 30th, 2010 @ 06:18 AM

  2. Norman Clarke
    Norman Clarke
    • Tag changed from activesupport, chars, multibyte, patch to activesupport, chars, multibyte, patch, verified
    • Assigned user set to José Valim

    This no longer applies to Rails master after recent changes; here's an updated patch that now applies.

    +1 on this, it's a simple change that makes the behavior of the proxy class match Ruby's String#slice.

    June 30th, 2010 @ 01:51 PM

  3. Repository
    Repository
    • State changed from new to resolved

    (from [265b7c5edfe9b60d1ab547bfef569d94df05b8e9]) Fix ActiveSupport::Multibyte::Chars#slice for empty strings when starting offset is negative [#4717 [PATCH] Fix ActiveSupport::Multibyte::Chars#slice for empty strings when star... state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/265b7c5edfe9b60d1ab547bfef569d...

    June 30th, 2010 @ 02:09 PM