Question about string concatenation

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

···

--
Posted via http://www.ruby-forum.com/.

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 `<main>'

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

···

On Sun, Jan 20, 2013 at 3:08 PM, David Richards <lists@ruby-forum.com> 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?

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.

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

···

On 20.01.2013 21:08, David Richards wrote:

I'm puzzled about why the following happens (I'm using v1.9.3):

s = "[" 9.to_s "]" # error

just make your expression string friendly, eg

"[" "#{9.to_s}" "]"
=> "[9]"

best regards -botp

···

On Mon, Jan 21, 2013 at 4:08 AM, David Richards <lists@ruby-forum.com>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 << "]"

Dave Aronson 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

···

--
Posted via http://www.ruby-forum.com/\.

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 6:12 AM, botp <botpena@gmail.com> wrote:

On Mon, Jan 21, 2013 at 4:08 AM, David Richards <lists@ruby-forum.com> > 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]"

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

botp wrote in post #1092991:

···

On Mon, Jan 21, 2013 at 4:08 AM, David Richards > <lists@ruby-forum.com>wrote:

just make your expression string friendly, eg

"[" "#{9.to_s}" "]"
=> "[9]"

best regards -botp

Nice thread it is. `+1` to you.

--
Posted via http://www.ruby-forum.com/\.

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 Sun, Jan 20, 2013 at 9:44 PM, David Richards <lists@ruby-forum.com> wrote:

Dave Aronson 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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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 3:00 PM, Robert Klemme <shortcutter@googlemail.com>wrote:

You do not need .to_s inside string interpolation. But what is the
point in suggesting that over this solution?

s = "[#{9}]"

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 8:15 AM, botp <botpena@gmail.com> wrote:

On Mon, Jan 21, 2013 at 3:00 PM, Robert Klemme <shortcutter@googlemail.com> > 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)..

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

oops, mea culpa =)
should be any anyexpression then, eg
"[" "#{anyexpression}" "]"

best regards -botp

···

On Mon, Jan 21, 2013 at 6:01 PM, Robert Klemme <shortcutter@googlemail.com>wrote:

But the point of string interpolation is that the expression does
_not_ need to return a String because #to_s is applied automatically.