From: "NARUSE, Yui" Date: 2009-12-17T11:20:32+09:00 Subject: [ruby-core:27204] Re: [PATCH] fix CGI::escape to work with blocks, avoid dollar variables --000e0cd1132acaa3ef047ae341f1 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Dec 17, 2009 at 12:28 AM, Gaston Ramos wrote: > Hi Ruby-Core, I attach a path that solve this problem: > > class String > alias :old_gsub :gsub > def gsub(*args, &block) > old_gsub(*args, &block) > end > end > > require 'cgi' > CGI.escape("An Interview with Criteri…") > > NoMethodError: undefined method `size' for nil:NilClass > from /usr/lib/ruby/1.8/cgi.rb:343:in `escape' > from (irb):4:in `old_gsub' > from (irb):4:in `gsub' > from /usr/lib/ruby/1.8/cgi.rb:342:in `escape' > from (irb):9 > > As you can see CGI.escape won't work well when we use gsub and blocks, this > issue was discussed in this thread: > > > http://www.justskins.com/forums/bug-when-rerouting-string-gsub-with-a-block-using-1-a-52852.html Even if you patch cgi.rb, you can use only matched string ($&) which is passed as block parameter. There are $~ (Regexp.last_match), $` (pre_match), $' (post_match) and $1, $2, $3 and so on. So your patch is fortunately adopted to cgi.rb, but not universal. So I'm against such a sectional patch. > where Matz say "Then don't use ugly dollar variables" When Matz say so? That statement doesn't face up to real situation, described above. dollar variables may be ugly but required. > so, dollar variables are > problematic in this case, I changed the dollar variables by a block > argument > variable and the problem was solved, I added 2 tests for CGI.escape and > CGI.unescape that reproduce this "problem", If you agree with this patch I > could > do other new patch in order to avoid all dollar variables inside CGI lib. > > And there is a bug in ActiveSupport realted with this issue: > > https://rails.lighthouseapp.com/projects/8994/tickets/3475-activesupportmultibytecharsgsub-fails-while-stringgsub-works > So current answer is "Don't change String#gsub when you controll all things". See also this ticket (but this is in Japanese...) http://redmine.ruby-lang.org/issues/show/546 -- NARUSE, Yui naruse@airemix.jp --000e0cd1132acaa3ef047ae341f1 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Thu, Dec 17, 2009 at 12:28 AM, Gaston Ramos <= span dir=3D"ltr"><ramos.gaston= @gmail.com> wrote:
Hi Ruby-Core, I attach a path that solve this problem:

class String
=A0alias :old_gsub :gsub
=A0 =A0def gsub(*args, &block)
=A0 =A0 =A0 old_gsub(*args, &block)
=A0 =A0end
end

require 'cgi'
CGI.escape("An Interview with Criteri&#8230;")

NoMethodError: undefined method `size' for nil:NilClass
=A0from /usr/lib/ruby/1.8/cgi.rb:343:in `escape'
=A0from (irb):4:in `old_gsub'
=A0from (irb):4:in `gsub'
=A0from /usr/lib/ruby/1.8/cgi.rb:342:in `escape'
from (irb):9

As you can see CGI.escape won't work well when we use gsub and blocks, = this
issue was discussed in this thread:

http://www.justskins.co= m/forums/bug-when-rerouting-string-gsub-with-a-block-using-1-a-52852.html

Even if you patch cgi.rb, you can use only matched= string ($&) which is passed as block parameter.
There are $~= (Regexp.last_match), $` (pre_match), $' (post_match) and $1, $2, $3 an= d so on.
So your patch is fortunately adopted to cgi.rb, but not universal.

So I'm against such a sectional patch.
=A0
where Matz say "Then don't use ugly dollar variables"

When Matz say so?
That statement doesn&#= 39;t face up to real situation, described above.
dollar variables= may be ugly but required.
=A0
so, dollar variables are
problematic in this case, I changed the dollar variables by a block argumen= t
variable and the problem was solved, I added 2 tests for CGI.escape and
CGI.unescape that reproduce this "problem", If you agree with thi= s patch I could
do other new patch in order to avoid all dollar variables inside CGI lib.
And there is a bug in ActiveSupport realted with this issue:
= https://rails.lighthouseapp.com/projects/8994/tickets/3475-activesupportmul= tibytecharsgsub-fails-while-stringgsub-works

So current answer is "Don't chan= ge String#gsub when you controll all things".

See a= lso this ticket (but this is in Japanese...)
http://redmine.ruby-lang.org/issues/sh= ow/546

--
NARUSE, Yui
naruse@airem= ix.jp
--000e0cd1132acaa3ef047ae341f1--