Bug in Ruby with adding strings?

I just found what looks like a bug in Ruby 184-20. (I haven't tried it
in newer builds.) Here's a simple script that will expose this bug:

···

----
irb(main):001:0> x = 1
=> 1
irb(main):002:0> puts 'foo' + x.to_s +'bar'
SyntaxError: compile error
(irb):2: syntax error
puts 'foo' + x.to_s +'bar'
                     ^
        from (irb):2
----

I finally figured out that it was the Plus sign *right next* to the
"bar", *after* the "to_s" method that Ruby dislikes. If I insert a
space, I get the correct output. If I take out both spaces, I get the
correct output. It's only if there's a space after the "to_s" and none
between the Plus sign and string-in-quotes that it blows up.

That is:

puts 'foo' + x.to_s + 'bar' # this works
puts 'foo' + x.to_s +'bar' # doesn't work
puts 'foo' + x.to_s+ 'bar' # this works
puts 'foo' + x.to_s+'bar' # this works

Anyone know why this might be? I haven't played with it anymore, so I
wonder if this bug exists with other methods.

Paul

I suspect that having no space after the + makes Ruby think it is a
unary operator. And since to_s is a method, Ruby thinks that you're
passing +'bar' to x.to_s. This can't be the only problem, though,
since it is giving you a SyntaxError before it actually complains about
passing an unexpected argument to to_s...

    David

···

On Jan 19, 1:52 pm, "Paul" <tester.p...@gmail.com> wrote:

I just found what looks like a bug in Ruby 184-20. (I haven't tried it
in newer builds.) Here's a simple script that will expose this bug:
----
irb(main):001:0> x = 1
=> 1
irb(main):002:0> puts 'foo' + x.to_s +'bar'
SyntaxError: compile error
(irb):2: syntax error
puts 'foo' + x.to_s +'bar'
                     ^
        from (irb):2
----

I finally figured out that it was the Plus sign *right next* to the
"bar", *after* the "to_s" method that Ruby dislikes. If I insert a
space, I get the correct output. If I take out both spaces, I get the
correct output. It's only if there's a space after the "to_s" and none
between the Plus sign and string-in-quotes that it blows up.

That is:

> puts 'foo' + x.to_s + 'bar' # this works
> puts 'foo' + x.to_s +'bar' # doesn't work
> puts 'foo' + x.to_s+ 'bar' # this works
> puts 'foo' + x.to_s+'bar' # this worksAnyone know why this might be? I haven't played with it anymore, so I
wonder if this bug exists with other methods.

Paul

I think it's trying to parse as

puts('foo' + x.to_s, +'bar')

where the comma is missing. So it thinks +'bar' is not an argument to to_s but to puts.

Regards

  robert

···

On 19.01.2007 23:07, dflanagan@gmail.com wrote:

I suspect that having no space after the + makes Ruby think it is a
unary operator. And since to_s is a method, Ruby thinks that you're
passing +'bar' to x.to_s. This can't be the only problem, though,
since it is giving you a SyntaxError before it actually complains about
passing an unexpected argument to to_s...

Robert Klemme wrote:

I think it's trying to parse as
puts('foo' + x.to_s, +'bar')

where the comma is missing. So it thinks +'bar' is not an argument to
to_s but to puts.

I originally came across this when I was trying to assign something
similar to a variable (trying to create a filename).

That is: filename = 'foo' + x.to_s +'bar'

Would it still be trying to use the +'bar' as an argument when trying
to do an assignment?

Dunno. Maybe. This is valid Ruby:

irb(main):001:0> a = 1,2,3
=> [1, 2, 3]

So you *can* have multiple values on the right (as well as on the left).

Kind regards

  robert

···

On 20.01.2007 20:07, Paul wrote:

Robert Klemme wrote:

I think it's trying to parse as
puts('foo' + x.to_s, +'bar')

where the comma is missing. So it thinks +'bar' is not an argument to
to_s but to puts.

I originally came across this when I was trying to assign something
similar to a variable (trying to create a filename).

That is: filename = 'foo' + x.to_s +'bar'

Would it still be trying to use the +'bar' as an argument when trying
to do an assignment?

it may be parsing as 'foo' + x.to_s(+'bar') since to_s can sometimes
take an argument.

···

On 1/20/07, Paul <tester.paul@gmail.com> wrote:

Robert Klemme wrote:
>
> I think it's trying to parse as
> puts('foo' + x.to_s, +'bar')
>
> where the comma is missing. So it thinks +'bar' is not an argument to
> to_s but to puts.
>

I originally came across this when I was trying to assign something
similar to a variable (trying to create a filename).

That is: filename = 'foo' + x.to_s +'bar'

Would it still be trying to use the +'bar' as an argument when trying
to do an assignment?

Thanks for the feedback Robert. I didn't know Ruby could do that.
I'll just try to be more consistent with spaces and plus signs to avoid
this problem again.

Cheers. Paul. =)

Robert Klemme wrote:

···

Dunno. Maybe. This is valid Ruby:

irb(main):001:0> a = 1,2,3
=> [1, 2, 3]

So you *can* have multiple values on the right (as well as on the left).

Kind regards

  robert

Gregory Brown schrieb:

it may be parsing as 'foo' + x.to_s(+'bar') since to_s can sometimes
take an argument.

This makes sense. When translating the infix notation to method usage (don't know if this will be done by the interpreter), the first step is

'foo' + x.to_s +'bar' => 'foo'.+(x.to_s +'bar')

"String#+" has one argument, so "x.to_s +'bar'" must be one argument. When looking to "x" it is not clear for the interpreter, what the result of "x.to_s" will be, but it knows, that "to_s" has an (optional) argument.

So it tries to find "String#+@" method for 'bar' and fails.

The possibility to backtrack after failure and try first to evaluate "x.to_s", which returns the string "'1'", an then to compute "'1' +'bar'" (works without any problem) will not be done due to good reasons for the general case.

Wolfgang Nádasi-Donner