R
Roger Pack
Shouldn't the following be a syntax error?
=> " 34"
?
-rp
=> " 34"
?
-rp
Shouldn't the following be a syntax error?
=20
=3D> " 34"
Rarely seen, but string literal "implicit" concatenation has always been a
feature.
Rarely seen, but string literal "implicit" concatenation has always been
a feature.
Yeah--for me it has almost always represented a bug (like a missing ,
between parameters or what not).
Ahh well.
2010/2/19 Raul Jara said:This doesn't work if you assign the strings to variables though:
'hi ' 'there'
=3D>"hi there"
hi =3D 'hi '
there =3D 'there'
hi there
NoMethodError: undefined method `hi' for main:Object
=A0from (irb):9
=A0from /opt/local/bin/irb:12:in `<main>'
Which is a little counter intuitive.
Not for me. =A0The concatenation is done at parse time. =A0Your
"whitespace concatenation" is done at runtime.
Robert said:Not for me. The concatenation is done at parse time. Your
"whitespace concatenation" is done at runtime.
Kind regards
robert
I guess I find it counter intuitive to have such different behaviors
parse time vs. run time.
If it isn't suppose to be a behavior of
strings that you can stick one next to another and have them
concatenate, then my brain has a hard time understanding why the parser
should treat them as though they do have that behavior?
$
There is no way the parser can disambiguate concatenation of strings
referenced through variables and method invocations whereas a
concatenation of string literals is easily detectable.
Apart from that, I believe it is quite common in programming languages
to allow concatenation of string literals although that feature might be
rarely used.
Kind regards
robert
I think you misunderstand what I was saying. I was not asking what the
differences between the parser and the runtime are. I was asking for a
rationale. If Matz believed that strings, when placed together without
any punctuation between them should be concatenated, why didn't he make
it standard operating behavior? I can't imagine it would have been so
difficult for when the parser sees two variables in a row to treat it as
equivalent to a concatenation call on the first variable with the second
variable as an argument. If Matz didn't think that two strings should
be concatenated, why program that behavior into the parser?
Roger said:Yeah it ends up being pretty hard to generate a parser that will do
both.
-rp
Raul said:I'm not sure why, though. The parser is able to turn
x + y
into
x.+(y)
What would be so hard about having the parser turn:
x y
into
x.+(y)
?
Or if you only think strings should work that way, and not numbers:
x.concat(y)
["one" "two", "three"]
seems like it should really throw an error to me.
Justin said:Because
x y
already means
x(y)
-Justin
But the parser knows what's a method and what's a variable, right? (I'm
asking. =A0I assume so but I really don't know.) =A0If it knows what is a
variable and what is a method, it can define x y as x(y) when x is a
method, and x y as x.+(y) when x is a variable. =A0Right?
Rick said:In the expression
x y
It sees x as a method because of the form of the expression.
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.