Can anyone help me with this Ruby script?

I am very new to ruby and I was hoping someone could help me. In the following script I am asking the user their favorite number. I would then like to add a one to their favorite number and suggest the resultant number as a better favorite number.

puts ‘What\’s your favorite number?’ number = gets.chomp.to_s puts ‘Your favorite number is ‘+number+’.’

If anyone could tell me how to this it would be great!

Answer #1

Hi,

Well, this site was designed with Ruby, so you may want to FunMail the programmer. His username on here is esconsult1. He can probably help you :)

Answer #2

puts ‘What\’s your favorite number?’ number = gets.chomp.to_s

new_number = number.to_i + 1

puts ‘Your favorite number is ‘+new_number+’.’

Answer #3

To explain a little bit more:

new_number = number.to_i + 1

Converts “number” which you converted to a string before with “gets.chomp.to_s” (you read the input from the terminal and converted it to a string so it can be appended to the other string that you are printing out with the other “puts” statement. Fine.

However, a short way to do all that is also:

number = gets.chomp.to_i + 1 puts ‘ A better number is ‘ + number.to_s

More Like This
Funadvice Logo

© FUNADVICE 2025 · 212bydesign LLC

Answering questions since 2006

[email protected]

Miami, Florida, USA