Learning Ruby -- Lesson 3: Arithmetic Operators
Arithmetic Operations are going to be the first methods you learn about in these lessons. Just like the name implies, arithmetic operations are simple mathematical operations: adding, subtracting, multiplying, dividing, etc.
These are method calls that can be invoked on various data types (mainly numeric types, like floats or integers)
+ | Addition |
- | Subtraction |
/ | Division |
* | Multiplication |
% | Modulus |
** | Exponents |
Okay, time to put these to use! Open up a command prompt, and type
irb
IF YOU OPTED OUT OF INSTALLING RUBY, SIMPLY PUT MY EXAMPLES INTO THE SCRIPT EDITOR AND ADD
p
BEFORE EACH EXAMPLE AND TEST PLAY (if you added foreverZer0's console script, you can use puts instead of p)
If Ruby was installed correctly, you should get a new prompt that looks like this:
irb(main):001:0>
Let's try some out.
irb(main):001:0> 1 + 1 => 2
1 + 1 = 2. Good, it seems to be working correctly. Now, you can use irb as a calculator!
Let's play around with this a bit! The best way to learn (IMO) is to experiment on your own. So feel free to try different operations than me.
irb(main):002:0> 10000+250 => 10250 irb(main):003:0> 10 * 100 => 1000 irb(main):004:0> 100 / 10 => 10 irb(main):005:0> 30 - 100 => -70 irb(main):006:0> 10.5 + 0.777 => 11.277 irb(main):007:0> 10 + 10 * 2 => 30
Now, wait a second... 10 + 10 = 20, 20 * 2 = 40... How come 30?
You probably already guessed it...but, remember bedmas or order of precedence? That's right, bedmas applies to programming as well. Let me refresh your memory:
1. Brackets
2. Exponents
3. Division
4. Multiplication
5. Addition
6. Subtraction
So, it would be interpreted as 10 * 2 = 20, 20 + 10 = 30. Now what if we want ruby to read it the other way? Well, brackets work here too:
irb(main):008:0> (10 + 10) * 2 => 40
How about this?
irb(main):009:0> 3 / 2 => 1
Wait, 3 divided by 2 is NOT 1. Here is a little quirk you need to know about programming: Integer numbers and Floating point numbers are 2 distinct numeric types. They can be used together (adding, dividing, etc), however, if you divide and integer by an integer, you will ALWAYS get an integer back.
irb(main):010:0> 3 / 2.0 => 1.5
So what does this mean? Well, if operate between integers and floats, you should know that you will always get back a float. Operating with just integers will send back integers, and of course, floats if you are operating with just floats.
irb(main):011:0> 1.5+1 => 2.5 irb(main):012:0> 1.02 * 5 => 5.1
What about modulus? You may know what modulus is, but just in case:
Modulus is a form a division (therefore, it falls into the same area of precedence as multiplication and division). How it works, is modulus divides 2 numbers and returns the remainder.
I included the division operation in these examples, to help give you a full understanding of how it works:
irb(main):001:0> 3 / 2 => 1 irb(main):002:0> 3 % 2 => 1
2 goes into 3: 1 time, with 1 leftover
irb(main):003:0> 1 / 1 => 1 irb(main):004:0> 1 % 1 => 0
1 goes into 1: 1 time, with 0 leftover
irb(main):005:0> 1 / 5 => 0 irb(main):006:0> 1 % 5 => 1
5 goes into 1: 0 times, with 1 leftover
What happens when you add strings together? Or multiply them with integers?
irb(main):008:0> "hello" + ", world!" => "hello, world!" irb(main):009:0> "*" * 25 => "*************************" irb(main):010:0>
1 Comment
Recommended Comments
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now