Gsub with match replacement

I’m trying to convert

“12:34” => “12:34”

(I know there is a CGI module with unescapeHTML(), I’m working in a
ruby environment that does not allow for additions of modules)

I’m trying:

"12:34".gsub(/&#(\d+);/,'\1'.to_i.chr)

But I get “1234”

Can anyone explain what I’m doing wrong?

BTW I know I can do:

"12:34".gsub(/&#(\d+);/) do
Integer($1).chr
end

but for some reason in the ruby environment I’m working in
I get ‘Undefined method Integer for main:Object’

···


James R. Leu
jleu at internetnoc.com
iNOC -> http://internetnoc.com/

I’m trying to convert

“12:34” => “12:34”

(I know there is a CGI module with unescapeHTML(), I’m working in a
ruby environment that does not allow for additions of modules)

I’m trying:

"12:34".gsub(/&#(\d+);/,'\1'.to_i.chr)

But I get “1234”

Can anyone explain what I’m doing wrong?

When ruby is about to call method #gsub, it evaluates the arguments.
The second argument is ‘\1’.to_i.chr → 0.chr → “\000”.

BTW I know I can do:

"12:34".gsub(/&#(\d+);/) do

Integer($1).chr
end

but for some reason in the ruby environment I’m working in
I get ‘Undefined method Integer for main:Object’

It’s very strange that you get that error (works here). Maybe you can try
with $1.to_i.chr, as before.

I’m trying to convert

“12:34” => “12:34”

(I know there is a CGI module with unescapeHTML(), I’m working in a
ruby environment that does not allow for additions of modules)

I’m trying:

"12:34".gsub(/&#(\d+);/,'\1'.to_i.chr)

But I get “1234”

Can anyone explain what I’m doing wrong?

That doesn’t work because to_i is being called on the literal string ‘\1’ -
before gsub ever sees it and replaces ‘\1’ with the matching portion of the
string.

BTW I know I can do:

"12:34".gsub(/&#(\d+);/) do

Integer($1).chr
end

but for some reason in the ruby environment I’m working in
I get ‘Undefined method Integer for main:Object’

What version of Ruby? (You can look at the constant RUBY_VERSION).
But this should work:

    "12:34".gsub(/&#(\d+);/) { $1.to_i.chr }

-Mark

···

On Wed, Feb 04, 2004 at 02:05:44AM +0900, James R. Leu wrote: