Forum: Ruby Re: Newbie help, please

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Peter Z. (Guest)
on 2009-04-27 00:03
(Received via mailing list)
Mike W. wrote:
> Hello all,
> I am a complete novice to programming. I have chosen Ruby as my first
> language.
> I am attempting to follow some tutorials in a book I have. However, I am
> running into some errors, and can not figure out why. Can you please
> guide me?

You cannot simply add strings and numbers together; you must convert
number to string first.

Try this code:

puts (2+4).to_s + ' is the sum of 2+4.'


puts 'Hello, what is your favorite number?'
number = gets.chomp
better = number.to_i+1
puts number.to_s + '?? Don\t you know that ' + better.to_s + 'is a

But the right way to do this is to use expression inlining, such as

puts "#{2+4} is the sum of 2+4."


puts "#{number}?? Don\t you know that #{better} is a superior

It does .to_s implicitly. Notice the double quotes, it won't work with

     WBR, Peter Z.
This topic is locked and can not be replied to.