On Nov 26, 2013, at 4:15 PM, Joshua P. <lists / ruby-forum.com> wrote:

> I'm reading Chris Pines 'How to program' This was the latest concept,
> Length:
>=20
> "Another string method is length, which tells us the number of
> characters
> (including spaces) in the string:
> puts 'What is your full name?'
> name =3D gets.chomp
> puts 'Did you know there are ' + name.length + ' characters'
> puts 'in your name, ' + name + '?'
> What is your full name?
> Christopher David Pine
> #<TypeError: can't convert Fixnum into String>
>=20
> Uh-oh! See? There it is! It=92s an easy mistake to make. Anyway, if
> you didn=92t know to be on the lookout for this error, you can still
> figure
> that the problem must have happened sometime after the line name =3D
> gets.chomp, since I was able to type my name. See whether you can
> figure it out.
> The problem is with length: it gives us an integer, but we want a
> string.
> That=92s easy enough; we=92ll just throw in a .to_s (and cross our =
fingers"
>=20
> I don't see the problem with the computer giving us the integer =
version
> of the number of characters? He then asks us to create a program
> that will ask someone their first/middle/last names individually and
> then add them up after. I would think to just ask for them using GETS
> and then add up the lengths using the .to_s conversion.. but surely =
what
> I've learned about strings is that you cant add them together right?
>=20
> Can anyone explain this?
>=20
> Kind regards
>=20
> Josh
>=20
> --=20
> Posted via http://www.ruby-forum.com/.

Let=92s break this down into simpler bits:

    "hello," + " world"

concatenates (appends) the strings together, producing "hello, world"

    name =3D "Tamara"
    "hello, " + name=20

again concatenates the strings together, producing "hello, Tamara"

    name + ", your name contains " + name.length + " characters"

produces the error like in Chris=92s code, because it it mixing string =
concatenation using + with a number produced by name.length. Robert has =
explained why that breaks.

So to fix it, all those things need to be Strings. Chris is telling you =
the .to_s method is what converts the number from name.length into a =
String, so all of them can be concatenated together:

    name + ", your name contains " + name.length.to_s + " characters"

finally gives us "Tamara, your name contains 6 characters"