Bug? (1.8.0)

E

Erik Veenstra

Ruby 1.8.0 on win32:

1: ruby -e 'p "#abc#def#".gsub(/#$/, " ")'
-e:1: unterminated string meets end of file
-e:1: syntax error

2: ruby -e 'p "#abc#def#".gsub(/#/, " ")'
" abc def "

3: ruby -e 'p "#abc#def#".gsub(/\#$/, " ")'
"#abc#def "

What's wrong with the first one? I expected the first one to
behave like the third one.

gegroet,
Erik V.
 
H

Hal Fulton

Erik said:
Ruby 1.8.0 on win32:

1: ruby -e 'p "#abc#def#".gsub(/#$/, " ")'
-e:1: unterminated string meets end of file
-e:1: syntax error

2: ruby -e 'p "#abc#def#".gsub(/#/, " ")'
" abc def "

3: ruby -e 'p "#abc#def#".gsub(/\#$/, " ")'
"#abc#def "

What's wrong with the first one? I expected the first one to
behave like the third one.

That's interesting and syntactically subtle.

Here are the facts the explain this:
1. Regexes can have interpolated values just like strings
2. When an expression is a single name and that name starts
with a special symbol like @, @@, or $, the braces can
be omitted
3. $/ is one of the ugly special variables
4. So #$/ is interpreted as inserting the value of $/ into
the regex
5. The regex then consumes the rest of the line
6. The interpreter apparently does not distinguish between
strings and regular expressions when reporting that
something is unterminated.

If there is a bug at all, it is #6 (misleading error message).

Hal
 
D

Daniel Carrera

That's interesting and syntactically subtle.
Here are the facts the explain this:
1. Regexes can have interpolated values just like strings
2. When an expression is a single name and that name starts
with a special symbol like @, @@, or $, the braces can
be omitted
3. $/ is one of the ugly special variables
4. So #$/ is interpreted as inserting the value of $/ into
the regex
5. The regex then consumes the rest of the line
6. The interpreter apparently does not distinguish between
strings and regular expressions when reporting that
something is unterminated.

I'm not sure I followed that. Can you explain to me how 1-6 explain
this?:
SyntaxError: compile error
(irb):22: unterminated string meets end of file
from (irb):22



--
Daniel Carrera, Math PhD student at UMD. PGP KeyID: 9AF77A88
.-"~~~"-.
/ O O \ ATTENTION ALL PASCAL USERS:
: s :
\ \___/ / To commemorate the anniversary of Blaise Pascal's
`-.___.-' birth (today) all your programs will run at half speed.
 
H

Hal Fulton

Daniel said:
I'm not sure I followed that. Can you explain to me how 1-6 explain
this?:



SyntaxError: compile error
(irb):22: unterminated string meets end of file
from (irb):22

Isn't $" also a special variable?

Hal
 
D

Daniel Carrera

p "#$"
Isn't $" also a special variable?

Okay, I would suggest the following behaviour would make most sense:

p "#$" => '#$'
p "#{$"}" => contents of $"

Thoughts?

--
Daniel Carrera, Math PhD student at UMD. PGP KeyID: 9AF77A88
.-"~~~"-.
/ O O \ ATTENTION ALL PASCAL USERS:
: s :
\ \___/ / To commemorate the anniversary of Blaise Pascal's
`-.___.-' birth (today) all your programs will run at half speed.
 
R

Rene van Bevern

I'd rather just see the ugly variables go away.

i totally agree with that. i also dislike cursing-variables. i'd like to
see the interpreter having an option to (de)activate those. so you don't
get to use them by accident.
_The Ruby Way_ intentionally avoided their use. There are a few
in there, but most are in code contributed by others.

can "The Ruby Way" be found online, like the pickaxe? (although i have
the pickaxe in "bookform"). I am very interesed in avoiding these
variables, but i only found a replacement for them with help of
MatchData. what about the others?
Rather than change the parser, I'd suggest that the nuby should
be aware that #$ will interpolate a global variable if the rest
of the variable name is valid.

i now wonder if this behavoir surprises matz? ;> (principe of matz'
least surprise)

Rene
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top