On May 12, 2006, at 7:05 PM, corey konrad wrote:

>
>> It printed out "5" just fine. Is that the problem you were  
>> referring to?
>>
>
> actually this one, i have to use + full_name.to_s + to avoid the  
> error i
> just found that strange. i mean i am using a string method the to_s
> should be implicit shouldnt it? i dont know.
>
> irb(main):030:0> def name_length
> irb(main):031:1>   p "what is your first name?"
> irb(main):032:1>   first = gets.chomp
> irb(main):033:1>   p "what is your last name?"
> irb(main):034:1>   last = gets.chomp
> irb(main):035:1>   full_length = first.length + last.length
> irb(main):036:1>   p "your full name has: " + full_length +  
> "characters"
> irb(main):037:1> end
> => nil
> irb(main):038:0> name_length
> "what is your first name?"
> corey
> "what is your last name?"
> konrad
> TypeError: can't convert Fixnum into String
> 	from (irb):36:in `+'
> 	from (irb):36:in `name_length'
> 	from (irb):38
> 	from :0
>
>
> -- 
> Posted via http://www.ruby-forum.com/.
>


Ok, puts <anything> works for anything.

str + <anything> does NOT work for anything.

str.length -> gives you a number

a number + number -> gives you a number

str + number -> gives you an error, before puts even get's around to  
seeing anything

I would suggest almost _always_ using interpolation. It will give you  
what you expect, pretty much everytime. To rewrite what you have  
above with string interpolation:
p "your full name has: #{name_length} characters"