Bug in Ruby with adding strings?

P

Paul

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
 
D

dflanagan

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
 
R

Robert Klemme

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

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
 
P

Paul

Robert said:
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?
 
R

Robert Klemme

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
 
P

Paul

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. =)
 
G

Gregory Brown

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.
 
W

Wolfgang Nádasi-Donner

Gregory said:
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
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top