Skip to content
cowmanifestation edited this page Sep 13, 2010 · 1 revision

Math

Numbers are the simplest way to begin experimentation with ruby.

Try something. In irb, type 2 + 2 and hit Enter. What comes back?

Try typing in some math problems. Ruby can do math, just like a calculator.

You can use + (“plus”), - (“minus”), * (“times”) and / (“divided by”).

Try using numbers with decimal points, and numbers without.

For example:

$ irb
irb(main):001:0> 3.0 + 2.3
=> 5.3
irb(main):002:0> 3 / 2
=> 1
irb(main):003:0> 3.0 / 2.0
=> 1.5
irb(main):004:0> 3.0 * 2.0
=> 6.0
irb(main):005:0> 3 * 2
=> 6

You might notice that when you enter a math problem in which at least one of the numbers have decimal points, the answer returned always has a decimal point, even if numbers to its right are zero. And if you enter a problem in which there are no decimal points, the value returned will have no decimal point as well; the answer will always be rounded down to the nearest integer.

That’s because they are two different kinds of arithmetic in Ruby: floating point (numbers with decimal points) and integer (numbers without). When given a calculation involving integers, Ruby rounds the answer down to the nearest integer if anything to the right of the decimal point is involved.

You can also use parentheses:

irb(main):001:0> (3 + 2) * (6 + 4)
=> 50
irb(main):002:0> 3 * (1 + 2)
=> 9

Enter exit to end your irb session. Now let’s open irb again, but this time, type irb –simple-prompt.

$ irb --simple-prompt
>>

You may find that you enjoy how this looks more than the previous system.

Let’s try some more experimentation with numbers.

>> 6.45 - 7.2
=> -0.75
>> .45 - 7
SyntaxError: compile error
(irb):2: no .<digit> floating literal anymore; put 0 before dot
.45 - 7
 ^
(irb):2: syntax error, unexpected '.'
.45 - 7
 ^
        from (irb):2
>> 0.45 - 7
=> -6.55
>>

As demonstrated in this example, Ruby doesn’t deal with numbers that have nothing (not even zero) to the left of the decimal point. It tells you exactly what the problem is, then prompts you again, giving you another chance.

What if you try dividing by zero?

>> 4 / 0
ZeroDivisionError: divided by 0
        from (irb):35:in `/'
        from (irb):35
        from :0
>> 4.0 / 0
=> Infinity

Either way, it’s not very useful to divide by zero.

You can also use ** to add an exponent. For example, 3 ** 2 is what we would call “3 to the power of 2” or “3 squared”.

>> 3 ** 2
=> 9
>> 3 ** 3
=> 27
>> 2 ** 1
=> 2
>> 2 ** 0
=> 1
>> 3 ** -2
=> 0.111111111111111

% is the modulo operator, which is meant to get you the remainder upon integer division. Entering 13 % 5 returns 3, because 5 goes into 13 twice and 3 is left over.

>> 19 % 2.5
=> 1.5
>> 18.0 % 3.75
=> 3.0
>> 16 % 5
=> 1
>> 20 % 7
=> 6
>> 29347 % 23471
=> 5876

Assignment

= is the assignment operator. Entering dogs = 4 assigns the value of 4 to the variable dogs

>> dogs = 4
=> 4
>> cats = 3
=> 3
>> dogs + cats
=> 7
>> dogs
=> 4
>> cats
=> 3
>> dogs += cats
=> 7
>> dogs
=> 7
>> cats
=> 3

You might have noticed that when dogs += cats was entered, the value of dogs was changed to 7. But when dogs and cats were simply added, the value was returned but the value of the individual variables was not changed. dogs += cats essentially expands out to dogs = dogs + cats.

>> cats += dogs
=> 10
>> cats
=> 10
>> dogs
=> 7

Your irb session will continue to keep track of the values of the two variables until you close it. When you open a new session, any variables you had created in a previous session do not exist.

$ irb --simple-prompt
>> dogs
NameError: undefined local variable or method `dogs' for main:Object
        from (irb):1
>> cats
NameError: undefined local variable or method `cats' for main:Object
        from (irb):2