I’m puzzled about why the following happens (I’m using v1.9.3):
s = “[” 9.to_s “]” # error
These other two concatenation constructs appear to work fine:
s = “[” + 9.to_s + “]”
s = “[” << 9.to_s << “]”
What’s the key concept I’m missing here?
_ Dave
I’m puzzled about why the following happens (I’m using v1.9.3):
s = “[” 9.to_s “]” # error
These other two concatenation constructs appear to work fine:
s = “[” + 9.to_s + “]”
s = “[” << 9.to_s << “]”
What’s the key concept I’m missing here?
_ Dave
On Sun, Jan 20, 2013 at 3:08 PM, David R. [email protected]
wrote:
I’m puzzled about why the following happens (I’m using v1.9.3):
s = “[” 9.to_s “]” # error
These other two concatenation constructs appear to work fine:
s = “[” + 9.to_s + “]”
s = “[” << 9.to_s << “]”What’s the key concept I’m missing here?
Looks like you’re trying to “autoconcatenate” three strings. (If that
wasn’t a word before, it is now!) This works fine IF they’re all
literals, like this:
1.9.3p194 :001 > “1” “2” “3”
=> “123”
1.9.3p194 :002 > “1” ‘2’ “3”
=> “123”
But if one of them is not a literal, Ruby won’t autoconcatenate it:
1.9.3p194 :003 > “1” 2.to_s “3”
SyntaxError: (irb):3: syntax error, unexpected tINTEGER, expecting $end
“1” 2.to_s “3”
^
from /Users/dave/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `’
My SWAG would be that autoconcatenation takes place at the parser
level, and using anything other than a literal would require actual
evaluation, so it can’t be done at that level, only after the thing is
actually evaluated. At that level, though, it breaks the Ruby syntax
rules, which require that you put some kind of operator between items.
That’s why your other examples do work, as you’ve inserted the + or
<< operators.
-Dave
Hello,
the thing you are puzzled about happens because you did not tell Ruby
what you want to do with the string “[”. You just placed a number after
it (the error message should have told you, that the parser encountered
a “tInteger” (your 9), but was expecting $end (what means that something
ending the line would be appropriate).
Did your confusion come from string interpolation? It looks somewhat
similar to what you wrote; it would look something like “[#{9.to_s}]”,
where the to_s is fully optional, since everything in between the { and
} will be converted to a string.
Regards,
Calvin
Dave A. wrote in post #1092970:
My SWAG would be that autoconcatenation takes place at the parser level.
That makes sense. I’ll accept that on ‘faith’ until I gain a deeper
understanding of Ruby.
Thanks!
_ Dave
On Mon, Jan 21, 2013 at 4:08 AM, David R.
[email protected]wrote:
I’m puzzled about why the following happens (I’m using v1.9.3):
s = “[” 9.to_s “]” # error
These other two concatenation constructs appear to work fine:
s = “[” + 9.to_s + “]”
s = “[” << 9.to_s << “]”
just make your expression string friendly, eg
“[” “#{9.to_s}” “]”
=> “[9]”
best regards -botp
On Sun, Jan 20, 2013 at 9:44 PM, David R. [email protected]
wrote:
Dave A. wrote in post #1092970:
My SWAG would be that autoconcatenation takes place at the parser level.
That makes sense. I’ll accept that on ‘faith’ until I gain a deeper
understanding of Ruby.
Probably the best thing to achieve what you wanted initially is this:
s = “[#{9}]”
Where “9” can be any expression really.
Kind regards
robert
On Mon, Jan 21, 2013 at 3:00 PM, Robert K.
[email protected]wrote:
You do not need .to_s inside string interpolation. But what is the
point in suggesting that over this solution?s = “[#{9}]”
sorry for the confusion, robert. i just wanted to show how to combine
auto
or implied concat w another expression (the to_s was just an example
given
by op… could be any method that returns string)…
best regards -botp
On Mon, Jan 21, 2013 at 6:12 AM, botp [email protected] wrote:
s = “[” << 9.to_s << “]”
just make your expression string friendly, eg
“[” “#{9.to_s}” “]”
=> “[9]”
You do not need .to_s inside string interpolation. But what is the
point in suggesting that over this solution?
s = “[#{9}]”
Kind regards
robert
On Mon, Jan 21, 2013 at 8:15 AM, botp [email protected] wrote:
sorry for the confusion, robert. i just wanted to show how to combine auto
or implied concat w another expression (the to_s was just an example given
by op… could be any method that returns string)…
But the point of string interpolation is that the expression does
not need to return a String because #to_s is applied automatically.
Cheers
robert
On Mon, Jan 21, 2013 at 6:01 PM, Robert K.
[email protected]wrote:
But the point of string interpolation is that the expression does
not need to return a String because #to_s is applied automatically.
oops, mea culpa =)
should be any anyexpression then, eg
“[” “#{anyexpression}” “]”
best regards -botp
botp wrote in post #1092991:
On Mon, Jan 21, 2013 at 4:08 AM, David R.
[email protected]wrote:
just make your expression string friendly, eg
“[” “#{9.to_s}” “]”
=> “[9]”best regards -botp
Nice thread it is. +1 to you.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs