QP School
Additions in Ruby - Printable Version

+- QP School (https://qomplainerzschool.lima-city.de)
+-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3)
+--- Forum: Ruby 3 Tutorials, exercises and examples (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=17)
+--- Thread: Additions in Ruby (/showthread.php?tid=3313)



Additions in Ruby - Qomplainerz - 03-14-2021

The easiest way to do additions in Ruby is done by passing the values as follows:


Quote: Wrote:puts 3 + 2


The output on the screen will be 5.

Another way is storing the values in variables and then adding one variable to the other as follows:


Quote: Wrote:num1 = 3
num2 = 2
res1 = num1 + num2
puts res1


The output on the screen will be 5, too.

In case we don't already know the values that should be added
we can ask the user to give us the numbers,
store the user inputs in variables
and then add one variable to another as follows:


Quote: Wrote:puts "Please enter a number here: "
num1 = gets.chomp.to_i
puts "Please enter another number here: "
num2 = gets.chomp.to_i
res1 = num1 + num2
puts res1


Now the output will depend on the user input.
Assumed the user typed 3 first and 2 later the result will be 5 as well.