# Bug in Ruby with adding strings?

**URL:** <https://www.ruby-forum.com/t/bug-in-ruby-with-adding-strings/82717>\
**Category:** Ruby\
**Created:** [January 19, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-with-adding-strings/82717 "2007-01-19T22:56:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 19, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-with-adding-strings/82717/1 "2007-01-19T22:56:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 19, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-with-adding-strings/82717/2 "2007-01-19T23:11:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 20, 2007, 9:56am UTC](https://www.ruby-forum.com/t/bug-in-ruby-with-adding-strings/82717/3 "2007-01-20T09:56:07Z")

</div>

On 19.01.2007 23:07, removed\_email\_address@domain.invalid 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…

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

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 20, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-with-adding-strings/82717/4 "2007-01-20T20:11:13Z")

</div>

Robert K. 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?

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 22, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-with-adding-strings/82717/5 "2007-01-22T20:16:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 22, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-with-adding-strings/82717/6 "2007-01-22T20:24:29Z")

</div>

On 1/20/07, Paul [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 21, 2007, 11:55am UTC](https://www.ruby-forum.com/t/bug-in-ruby-with-adding-strings/82717/7 "2007-01-21T11:55:42Z")

</div>

On 20.01.2007 20:07, Paul wrote:

> 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

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [January 22, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/bug-in-ruby-with-adding-strings/82717/8 "2007-01-22T21:00:35Z")

</div>

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