Forum: Ruby on Rails ruby exception

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
bogdan.ionescu (Guest)
on 2005-11-19 14:20
(Received via mailing list)
Is there a logical explanation for this?

1.to_s+"1" => "11"
1.to_s+ "1" => "11"
1.to_s + "1" => "11"
1.to_s +"1" => test.rb:2: undefined method `+@' for "1":String
(NoMethodError)

bogdan
Simon.Santoro (Guest)
on 2005-11-19 16:00
(Received via mailing list)
Bogdan I. wrote:
> Is there a logical explanation for this?
>
> 1.to_s+"1" => "11"
> 1.to_s+ "1" => "11"
> 1.to_s + "1" => "11"
> 1.to_s +"1" => test.rb:2: undefined method `+@' for "1":String
> (NoMethodError)

i think the + operator can not be applied to a string. only to a number.
what are you trying to do?

you can do
a = +1

but a = +"1" makes no sense to me
jochen (Guest)
on 2005-11-19 16:12
(Received via mailing list)
Bogdan I. wrote:
>Is there a logical explanation for this?
>
>1.to_s+"1" => "11"
>1.to_s+ "1" => "11"
>1.to_s + "1" => "11"
>1.to_s +"1" => test.rb:2: undefined method `+@' for "1":String
>(NoMethodError)

When a and b are both strings. Then

a + b  means: Call method '+' (binary plus) in string a and give b as
parameter
a +b   means: Call method '+@' (unary plus) on string b which doesn't
exist

+1 is the positive number 1, -1 is the negative number -1, but what
should +"some string" mean? So it is not defined.

Jochen
--
Jochen Topf  removed_email_address@domain.invalid  http://www.remote.org/jochen/
+49-721-388298
bogdan.ionescu (Guest)
on 2005-11-19 18:22
(Received via mailing list)
Nothing really, I ommited typing a space and that resulted into an
exception
which eventually I tracked down to that.
bogdan.ionescu (Guest)
on 2005-11-19 18:25
(Received via mailing list)
Thanks, I got it.
bogdan
This topic is locked and can not be replied to.