This project is archived and is in readonly mode.
ActiveSupport::Multibyte::Chars#gsub fails while String#gsub works
-
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(' ', '+') endActiveSupport::Multibyte::Chars invokes
#gsubon a wrapped_string usingmethod_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 endNow here's the real problem. Surprisingly, when you return from a function in Ruby, the value of the
$1variable 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" nilSo 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) -
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.
-
Santiago Pastorino
- State changed from open to stale