Calling methods, getting string and adding them

I was wondering why this is not being accepted? I get an error stating
that + is an unknown method.

I defined a Class and 3 methods within the class, and am merely calling
the methods and trying to add them together for display from one line:

m.ClassMethodString + " " + m.ClassMethodString + ": " +
m.ClassMethodInteger.to_s

···

--
Posted via http://www.ruby-forum.com/.

Please post your class, or even better the entire code =]

Sam

···

On 14/06/12 08:48, Michael Sung wrote:

I was wondering why this is not being accepted? I get an error stating
that + is an unknown method.

I defined a Class and 3 methods within the class, and am merely calling
the methods and trying to add them together for display from one line:

m.ClassMethodString + " " + m.ClassMethodString + ": " +
m.ClassMethodInteger.to_s

And the actual error... most likely your function isn't returning a
string. The error should tell you want class '+' is not defined in. For
example, if your method returns nil you'll get:

NoMethodError: undefined method `+' for nil:NilClass

Easiest fix would be to call to_s on the return value of your methods
(e.g. m.ClassMethodString.to_s) but that might merely cover-up some
problem with your method's return value.

-Paul

···

On Thu, 2012-06-14 at 05:54 +0900, Sam Duncan wrote:

On 14/06/12 08:48, Michael Sung wrote:
> I was wondering why this is not being accepted? I get an error stating
> that + is an unknown method.
>
> I defined a Class and 3 methods within the class, and am merely calling
> the methods and trying to add them together for display from one line:
>
>
> m.ClassMethodString + " " + m.ClassMethodString + ": " +
> m.ClassMethodInteger.to_s
>
Please post your class, or even better the entire code =]

No. The easiest fix is to use string interpolation.

···

On Jun 13, 2012, at 14:26 , Paul Sutton wrote:

Easiest fix would be to call to_s on the return value of your methods
(e.g. m.ClassMethodString.to_s) but that might merely cover-up some
problem with your method's return value.

Easiest is subjective - but yes, I think string interpolation is in
general a better solution when outputting a string.

···

On Thu, 2012-06-14 at 09:05 +0900, Ryan Davis wrote:

On Jun 13, 2012, at 14:26 , Paul Sutton wrote:

> Easiest fix would be to call to_s on the return value of your methods
> (e.g. m.ClassMethodString.to_s) but that might merely cover-up some
> problem with your method's return value.

No. The easiest fix is to use string interpolation.

It is measurable by the number of methods called and the number of garbage objects created.

···

On Jun 13, 2012, at 21:24 , Paul Sutton wrote:

Easiest is subjective - but yes, I think string interpolation is in
general a better solution when outputting a string.

Compare:

% ruby -rtracer -e '"a" + 1.to_s + "b" + 2.to_s + "c"'
#0:-e:1::-: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:Fixnum:>: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:Fixnum:<: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:String:>: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:String:<: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:String:>: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:String:<: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:Fixnum:>: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:Fixnum:<: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:String:>: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:String:<: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:String:>: "a" + 1.to_s + "b" + 2.to_s + "c"
#0:-e:1:String:<: "a" + 1.to_s + "b" + 2.to_s + "c"

2111 % ruby -rtracer -e '"a#{1}b#{2}c"'
#0:-e:1::-: "a#{1}b#{2}c"
#0:-e:1:Fixnum:>: "a#{1}b#{2}c"
#0:-e:1:Fixnum:<: "a#{1}b#{2}c"
#0:-e:1:Fixnum:>: "a#{1}b#{2}c"
#0:-e:1:Fixnum:<: "a#{1}b#{2}c"

···

On Jun 13, 2012, at 21:28 , Ryan Davis wrote:

On Jun 13, 2012, at 21:24 , Paul Sutton wrote:

Easiest is subjective - but yes, I think string interpolation is in
general a better solution when outputting a string.

It is measurable by the number of methods called and the number of garbage objects created.

>> Easiest is subjective - but yes, I think string interpolation is in
>> general a better solution when outputting a string.
>
> It is measurable by the number of methods called and the number of garbage objects created.

...

2111 % ruby -rtracer -e '"a#{1}b#{2}c"'
#0:-e:1::-: "a#{1}b#{2}c"
#0:-e:1:Fixnum:>: "a#{1}b#{2}c"
#0:-e:1:Fixnum:<: "a#{1}b#{2}c"
#0:-e:1:Fixnum:>: "a#{1}b#{2}c"
#0:-e:1:Fixnum:<: "a#{1}b#{2}c"

Thank you for the tracer analysis. I did some time benchmarks and it
seems to be about 30% faster. I agree completely that interpolation is
better.

-Paul

···

On Thu, 2012-06-14 at 13:32 +0900, Ryan Davis wrote:

On Jun 13, 2012, at 21:28 , Ryan Davis wrote:
> On Jun 13, 2012, at 21:24 , Paul Sutton wrote: