Welcome!


FunAdvice is a social question & answer site where you can make friends, share photos and meet people near you.
FunAdvice UK RSS for this page:
Rss_feed

Can anyone help me with this ruby script?

Asked by nicnicman about 1 year ago, 3 answers.
Send me Fun Mail

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!

Send this to a friend

Me! Answered by editor on Jul 11, 2007, 05:18AM
Send me Fun Mail | 6710 answers.

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

killface on adult swim Answered by esconsult1 on Jul 11, 2007, 06:00AM
Send me Fun Mail | 551 answers.

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

new_number = number.to_i + 1

puts 'Your favorite number is '+new_number+'.'

killface on adult swim Answered by esconsult1 on Jul 11, 2007, 06:05AM
Send me Fun Mail | 551 answers.

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

Answer this Question: "Can anyone help me with this Ruby script?"

Your Answer: HTML is not allowed.


Back to top

Most popular related questions