Forum: Ruby Re: Newbie help, please

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2a2ef2adb5f48cfec60ba3c3b381eead?d=identicon&s=25 Peter Zotov (Guest)
on 2009-04-26 22:03
(Received via mailing list)
Mike West 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.'

and

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
superior
selection?'

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

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

or

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

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

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