Ruby exception


#1

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


#2

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


#3

Nothing really, I ommited typing a space and that resulted into an
exception
which eventually I tracked down to that.


#4

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


#5

Thanks, I got it.
bogdan