Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Ruby Numbers2:36 with Jason Seifer
Did you know Ruby can also be used as a calculator? In this video, we're going to learn about numbers in Ruby as well as how to use irb as a simple calculator.
Assign a variable as a number (in irb):
a = 1 b = 2
Add two numbers together:
a + b
Subtract two numbers:
b - a
Try adding and subtracting different numbers using irb or writing your own Ruby program.
So far, we've been working pretty much exclusively with strings.
In Ruby, strings aren't the only things that we can work with.
We can also work with numbers.
And just like with strings, it's very, very easy to create a number.
The big difference is that we don't use quotes when creating numbers like we do
We'll just leave the quotes off.
We could actually use IRB to create a very simple calculator.
Let's go ahead and check that out to see how it works.
Let's go ahead and launch IRB now so that we can work with numbers.
The easiest way to work with a number inside of IRB is to just type it,
and we can see that Ruby creates a number for us.
If we wanted to add two numbers together, we could do that as well.
And Ruby will give us the output.
Now, just like with strings, we can assign numbers to variables as well.
So I'm just going to say a is 1.
And b is 2.
Now, instead of calling 1 + 2,
which still works, I can call a + b.
And that will return the two numbers added together.
If I wanted to, I could also assign that to a third variable.
Now I have a, b, and c, which is the value of a and b added together.
Luckily, we're not limited just to addition.
We could also subtract the two numbers.
We could do b-a.
And it will return 1.
We could also add.
And subtract all at the same time.
Now, we're not limited to just using whole numbers or integers.
If we wanted to, we could also work with decimals.
So we could say 1 + 1.1, and
Ruby would know how to work with that.
Go ahead and try experimenting with adding and
subtracting different numbers and assigning them to variables.
You need to sign up for Treehouse in order to download course files.Sign up