I’m new to Ruby and am currently working on some practice code which looks like the following:
puts 'Hello there, Can you tell me your favourite number?'
num = gets.chomp
puts 'Your favourite number is ' + num + '?'
puts 'Well its not bad but ' + num * 10 + ' is literally 10 times better!'
This code however just puts ten copies of the num variable and doesn’t actually multiply the number so I assume I need to make the ‘num’ variable an integer? I’ve had no success with this so can anyone show me where I’m going wrong please?
If you are using
to_i, thenchompbefore that is redundant. So you can do:But generally, using
"#{}"is better since you do not have to care aboutto_s, and it runs faster, and is easier to see. The methodString#+is particularly very slow.