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
Subtractions in Ruby
#1
The easiest way to do subtractions in Ruby is done by passing the values as follows:


Quote:puts 3 - 2


The output on the screen will be 1.

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


Quote:num1 = 3
num2 = 2
res1 = num1 - num2
puts res1


The output on the screen will be 1, too.

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


Quote: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 1 as well.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Everything you need to know about Ruby Qomplainerz 0 566 07-25-2023, 09:53 AM
Last Post: Qomplainerz
  Additions in Ruby Qomplainerz 0 1,586 03-14-2021, 10:13 AM
Last Post: Qomplainerz
  Predecessor in Ruby Qomplainerz 0 1,610 02-28-2021, 10:31 AM
Last Post: Qomplainerz
  Successor in Ruby Qomplainerz 0 1,552 02-28-2021, 10:29 AM
Last Post: Qomplainerz
  Least common multiple in Ruby Qomplainerz 0 1,648 02-28-2021, 09:11 AM
Last Post: Qomplainerz
  Floor and precisions in Ruby Qomplainerz 0 1,614 02-28-2021, 08:40 AM
Last Post: Qomplainerz
  Ceil and precisions in Ruby Qomplainerz 0 1,623 02-28-2021, 08:35 AM
Last Post: Qomplainerz
  Quotients and remainders in Ruby Qomplainerz 0 1,851 02-27-2021, 08:51 AM
Last Post: Qomplainerz
  Getting the currently installed Ruby version Qomplainerz 0 1,611 02-26-2021, 08:23 AM
Last Post: Qomplainerz
  Getting started with Ruby 3 Qomplainerz 0 1,897 02-26-2021, 07:34 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 2 Guest(s)