[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Bug in Ruby with adding strings?

Paul

1/19/2007 9:52:00 PM

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

10 Answers

dflanagan

1/19/2007 10:07:00 PM

0

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

Robert Klemme

1/20/2007 8:52:00 AM

0

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

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

Paul

1/20/2007 7:07:00 PM

0

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?

Robert Klemme

1/21/2007 10:51:00 AM

0

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?

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

Paul

1/22/2007 7:12:00 PM

0

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

1/22/2007 7:20:00 PM

0

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?

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

WoNáDo

1/22/2007 8:03:00 PM

0

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

TareeDawg

3/4/2013 12:16:00 PM

0

Gerard wrote:
> John Hood <johnhood@iinet.net.au> typed:
>> <tshivhasetshili998@gmail.com> wrote in message
>> news:0757314e-0c12-4027-a5c8-33af3cdc3e00@googlegroups.com...
>>> Hi
>>>
>>> I a doing a research on Hip hop and its effect on society, culture
>>> and would like your opinion on the topic.
>>
>> Would that be classical, romantic or baroque hip hop?
>>
>> JH
>
> Only HIP hip hop.
>

I do HIP but not the HOP.

Ray Hall, Taree

EM

3/4/2013 5:08:00 PM

0

Ray Hall <raymond.hall1@bigpond.com> - Mon, 04 Mar 2013 23:15:54
+1100:

> I do HIP but not the HOP.

Come on now.

https://www.youtube.com/watch?v=7...

;-)

EM

Bob Harper

3/5/2013 12:24:00 AM

0

On 3/4/13 3:35 AM, John Hood wrote:
> <tshivhasetshili998@gmail.com> wrote in message
> news:0757314e-0c12-4027-a5c8-33af3cdc3e00@googlegroups.com...
>> Hi
>>
>> I a doing a research on Hip hop and its effect on society, culture and
>> would like your opinion on the topic.
>
> Would that be classical, romantic or baroque hip hop?
>
> JH

I suspect he's interested in the Georgian variants.

Bob Harper