Syntax error (Re: multi-lang support in Ruby)

Hi Gavin,

I would trust that (\d+) will succesfully turn into an integer and forget
exception handling:

text.gsub!(!(/&#(\d+);/) { $1.to_i }

I don’t know what you could possibly have to rescue, anyway, because
$1 will always be a string in that context, so #to_i will succeed.

What I want to rescue here is when \d > 255 then $1.to_i.chr will report
error. I’m not just doing to_i, but to_i.chr. I fixed that according to a
help received from this mailing list – using unpack(“U”).

As a matter of style, I never use do … end on a single line. I
use do … end for “procedures” (i.e. do something) and { … }
for evaluation purposes.

You are absolutely right. I tend to ALWAYS use do … end. Partly because I
like pascal better than C, and partly because I don’t want too much aliases
for grammar, to keep my program simple. However, yesterday I found out that:

result=text.gsub(/(regexp)/) do $1.to_i.chr end

won’t work (i.e., no return value), but

result=text.gsub(/(regexp)/) { $1.to_i.chr } will…

Is ruby designed to function like that (i.e., different behavior between
do…end and {}), or is it a little accident?

Thanks
Shannon

···

Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail

Hi –

···

On Mon, 16 Dec 2002, Shannon Fang wrote:

You are absolutely right. I tend to ALWAYS use do … end. Partly because I
like pascal better than C, and partly because I don’t want too much aliases
for grammar, to keep my program simple. However, yesterday I found out that:

result=text.gsub(/(regexp)/) do $1.to_i.chr end

won’t work (i.e., no return value), but

result=text.gsub(/(regexp)/) { $1.to_i.chr } will…

Is ruby designed to function like that (i.e., different behavior between
do…end and {}), or is it a little accident?

Either should work fine:

irb(main):001:0> “abcde”.gsub(/(.)/) { $1.upcase }
“ABCDE”
irb(main):002:0> “abcde”.gsub(/(.)/) do $1.upcase end
“ABCDE”

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav