puts "Enter a number: " num1 = gets.chomp() puts "Enter another number" num2 = gets.chomp()
puts (num1 + num2)
Enter a number 5
Enter another number 2 52 Since it is a String, it cannot be added. Use to_i for resolution
puts "Enter a number: " num1 = gets.chomp() puts "Enter another number" num2 = gets.chomp()
puts (num1.to_i + num2.to_i)
Enter a number 5
Enter another number 2 7
Enter a number 5
Enter another number 2.5 7
Does not recognize after the decimal point. Use to_f for resolution puts (num1.to_f + num2.to_f)
Enter a number 5
Enter another number 2.5 7.5 The following is the cleaner source puts "Enter a number: " num1 = gets.chomp().to_f puts "Enter another number" num2 = gets.chomp().to_f
puts (num1 + num2)
Enter a number 5.5
Enter another number 2.1 7.6
Recommended Posts