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.

ActiveSupport::Multibyte::Chars#gsub fails while String#gsub works

#3475
>> s = "Hybrid Companies and the Future of the Economy: An Interview with Criteri…"
=> "Hybrid Companies and the Future of the Economy: An Interview with Criteri…"
>> require 'cgi'
=> true
>> CGI.escape(s)
=> "Hybrid+Companies+and+the+Future+of+the+Economy%3A+An+Interview+with+Criteri%26%238230%3B"
>> require 'activesupport'
=> true
>> s = ActiveSupport::Multibyte::Chars.new "Hybrid Companies and the Future of the Economy: An Interview with Criteri…"
=> #<ActiveSupport::Multibyte::Chars:0x1017be2e0 @wrapped_string="Hybrid Companies and the Future of the Economy: An Interview with Criteri&#8230;">
>> CGI.escape(s)
NoMethodError: undefined method `size' for nil:NilClass
    from /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/1.8/cgi.rb:343:in `escape'
    from /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/multibyte/chars.rb:100:in `gsub'
    from /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/multibyte/chars.rb:100:in `__send__'
    from /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/multibyte/chars.rb:100:in `method_missing'
    from /opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/1.8/cgi.rb:342:in `escape'

Reported by Mike Perham · November 10th, 2009 @ 03:35 AM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Norman Clarke
    Norman Clarke

    The problem here is that CGI.escape relies on the $1 variable, to get the last regexp match:

    # cgi.rb:341
    def CGI::escape(string)
      string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
        '%' + $1.unpack('H2' * $1.size).join('%').upcase
      end.tr(' ', '+')
    end
    

    ActiveSupport::Multibyte::Chars invokes #gsub on a wrapped_string using method_missing.

    # chars.rb:96
    # Forward all undefined methods to the wrapped string.
    def method_missing(method, *args, &block)
      if method.to_s =~ /!$/
        @wrapped_string.__send__(method, *args, &block)
        self
      else
        result = @wrapped_string.__send__(method, *args, &block)
        p $1
        result.kind_of?(String) ? chars(result) : result
      end
    end
    

    Now here's the real problem. Surprisingly, when you return from a function in Ruby, the value of the $1 variable is cleared:

    $ cat temp.rb
    def test_gsub
      result = "a".gsub(/(a)/, "b")
      p $1
      result
    end
    
    test_gsub
    p $1
    
    $ ruby temp.rb
    "a"
    nil
    

    So I don't think there's any decent way to fix this in ActiveSupport, ideally the fix would go in CGI.

    As a simple workaround, I suggest you simply do this:

    CGI.escape(s.to_s)
    

    April 13th, 2010 @ 02:50 PM

  2. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:50 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:50 PM

  4. bingbing