Baby's First Ruby App

So I’ve begun to learn Ruby (for work), and I thought I might share a little bit of my progress so far. Progress might be an exaggeration considering how basic this application actually is, but at least I’m slowly but surely moving in the right direction anyway.

So here’s the 1st part of it:

   1:  #GET INPUT
   2:  puts "Please enter a number"  
   3:  STDOUT.flush  
   4:  num1 = gets.chomp.to_i
   5:   
   6:  puts "Please enter another number"  
   7:  STDOUT.flush  
   8:  num2 = gets.chomp.to_i
   9:   
  10:  puts "Please pick the type of operation:
  11:  (A)dd
  12:  (S)ubtract
  13:  (M)ultiply
  14:  (D)ivide)"  
  15:  STDOUT.flush  
  16:  oper = gets.chomp
 

So here we’re asking the user for 2 numbers, cleaning up the input, and then storing it into 2 variables (num1 and num2). We then ask the user to select which type of mathematical operation they’d like to perform (addition, subtraction, multiplication, or division).

The 2nd part:

   1:  #SELECT THE OPERATION
   2:  if oper == ("a" or "A")  
   3:          add(num1, num2)
   4:      elsif oper == ("s" or "S")
   5:          subtract(num1, num2)
   6:      elsif oper == ("m" or "M")
   7:          multiply(num1, num2)
   8:      elsif oper ==  ("d" or "D")
   9:          divide(num1, num2)
  10:  end

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

So here we’re deciding what we’re going to do with our 2 variables (num1 and num2) based on the users selection. If they enter “a” or “A” (for example) we will then execute the method called “add” on line 3. Speaking of these methods…

The 3rd and final part:

   1:  #DEFINE THE METHODS
   2:  def add(num1, num2)  
   3:    sum = num1 + num2
   4:    puts "#{num1} + #{num2} = #{sum}"
   5:  end  
   6:   
   7:  def subtract(num1, num2)
   8:      if num1 > num2  
   9:        diff = num1 - num2
  10:        puts "#{num1} - #{num2} = #{diff}"
  11:      else
  12:        diff = num2 - num1
  13:        puts "#{num2} - #{num1} = #{diff}"
  14:      end
  15:  end  
  16:   
  17:  def multiply(num1, num2)  
  18:    prod = num1 * num2
  19:    puts "#{num1} X #{num2} = #{prod}"
  20:  end  
  21:   
  22:  def divide(num1, num2)  
  23:      if num1 > num2  
  24:          div = num1 / num2
  25:          puts "#{num1} /  #{num2} = #{div}"
  26:      else
  27:          div = num2/num1
  28:          puts "#{num2} /  #{num1} = #{div}"
  29:      end
  30:  end 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Each one of these methods (starting on lines 2, 7, 17, and 22) perform a particular set of functions based on the variables that are being passed in (num1 and num2). These methods are called based on the earlier code (part 2) that selected which method applied based on which of the mathematical operations the user selected. The 2 numbers that the user entered are passed into that method, some type of calculation is carried out, and then the result is displayed to the user. In the case of subtraction and division, I elected to add an additional step that would determine which of the 2 entered numbers was larger, and then it would order the calculation accordingly so as to not return a negative number (but this is purely optional).

To recap, and provide a plain English flow of how this happen in this simple little application: the user enters 2 numbers, and then they select a mathematical operation. Based on that operation we go through a series of checks to match which operation was selected, the 2 numbers entered by the users are passed to the method, and then we enter the applicable method. The method then performs the necessary calculations and displays the output to the user.

Pretty simple, not exactly rocket science, but it’s a start at least. I wanted to play with conditional statements a bit, as well as creating and calling my own methods. Next up will be to continue to experiment with arrays and hashes.
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }