Poll: Was this article helpful for you?
You do not have permission to vote in this poll.
Yes
100.00%
1 100.00%
No
0%
0 0%
Total 1 vote(s) 100%
* You voted for this item. [Show Results]

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Additions in Ruby
#1
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.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Messages In This Thread
Additions in Ruby - by Qomplainerz - 03-14-2021, 10:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Everything you need to know about Ruby Qomplainerz 0 858 07-25-2023, 09:53 AM
Last Post: Qomplainerz
  Subtractions in Ruby Qomplainerz 0 1,640 03-08-2021, 11:42 AM
Last Post: Qomplainerz
  Predecessor in Ruby Qomplainerz 0 1,760 02-28-2021, 10:31 AM
Last Post: Qomplainerz
  Successor in Ruby Qomplainerz 0 1,714 02-28-2021, 10:29 AM
Last Post: Qomplainerz
  Least common multiple in Ruby Qomplainerz 0 1,818 02-28-2021, 09:11 AM
Last Post: Qomplainerz
  Floor and precisions in Ruby Qomplainerz 0 1,768 02-28-2021, 08:40 AM
Last Post: Qomplainerz
  Ceil and precisions in Ruby Qomplainerz 0 1,779 02-28-2021, 08:35 AM
Last Post: Qomplainerz
  Quotients and remainders in Ruby Qomplainerz 0 2,010 02-27-2021, 08:51 AM
Last Post: Qomplainerz
  Getting the currently installed Ruby version Qomplainerz 0 1,773 02-26-2021, 08:23 AM
Last Post: Qomplainerz
  Getting started with Ruby 3 Qomplainerz 0 2,072 02-26-2021, 07:34 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 1 Guest(s)