If you are using to_i
, then chomp
before that is redundant. So you can do:
puts 'Hello there, Can you tell me your favourite number?'
num = gets.to_i
puts 'Your favourite number is ' + num.to_s + '?'
puts 'Well its not bad but ' + (num * 10).to_s + ' is literally 10 times better!'
But generally, using "#{}"
is better since you do not have to care about to_s
, and it runs faster, and is easier to see. The method String#+
is particularly very slow.
puts 'Hello there, Can you tell me your favourite number?'
num = gets.to_i
puts "Your favourite number is #{num}?"
puts "Well its not bad but #{num * 10} is literally 10 times better!"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…