Your Ruby is installed now. It is time to start typing in your text editor.
First of all, save this program. You have to assign a name to this program like calc.rb. If you would like to execute this program then type ruby calc.rb. The result will be 6.
BEGINNING WITH PUTS
The command ‘puts’ is used to type whatever is the next.
INTEGER AND FLOAT
Integers are the numbers which have no decimal points, whereas the numbers having decimal points are known as floats.
These are the examples of integers:
These are the examples of some of floats:
Mostly, floats are not used in programs. These have great use for educational reasons and 3D graphics.
For calculation, floats are used. In order to add or deduct something, we have to use the signs ‘+’ and ‘–‘. If we have to divide something, we use the sign ‘/’ and ‘*’ is used for multiplication purpose.
Here is an example for these signs:
puts 2.0 + 4.0
puts 3.0 * 4.0
puts 6.0 – 9.0
puts 7.0 / 2.0
The output will be like this:
It is not essential to give space in these programs. These are used only for easy understanding.
Here are some examples related to integers:
The result is given below:
A little bit confusion is present with the last division. When we have calculation having integer values then the decimal answers will be rounded off. We can use such values in our common use. For example, with $2 you can buy a chocolate. If you have 7$, then how many chocolates can you buy 3 chocolates or 3 and one half? The right answer is 3. 1$ is rounded off here.
puts 4 * (13-7) + -14
puts 90 + (8080 / (10*8)) * -52
LET’S GIVE A TRY:
Now make a program which can give you the answers of these:
• Total hours present in a month
• Total number of days in a decade
• Total number of cup of teas in life
More complex program:
The life you have passed is 1021 billion seconds then what will be your age?