Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Different Kinds of Numbers2:32 with Jason Seifer
Ruby needs to know certain things about numbers when working them. There can be several different types of numbers: decimals, integers, and floats are just a few that we'll be going over.
Types of Numbers in Ruby
When looking at the class of a number, you often see Fixnum or Float as the type. What’s the deal? I thought decimal numbers such as 2.1 and 3.75 are of the type Float, and whole numbers such as 2 and 3 are of the type Integer. What’s this Fixnum thing? In Ruby, an integer can be either a Fixnum or a Bignum. For you, this really isn’t important. Most of the numbers you see will be of the type Fixnum. However, if a number becomes large enough, Ruby classifies it as a Bignum. This behaviour allows Ruby to operate more efficiently, but, again, you don’t need to worry about it. Just remember that an integer (whole number) can be either a Fixnum or Bignum.
Ruby Numbers Documentation by Type
Ask a number what kind it is:
Returns the following:
Ask a decimal number what kind of number it is:
Returns the following:
Using a workspace, try different combinations of adding and subtracting strings and numbers. Don't worry about getting errors -- errors are part of programming.
Now just like with strings, when we called something like the upcase method,
We can do the same thing with numbers.
Let's go ahead and take a look at the differences between two numbers.
When I type a=1, I can ask Ruby how it's stored
inside of Ruby by asking it for its class name.
We don't need to worry exactly what classes are right now,
because we'll be getting into them much later on.
But for right now,
it is useful to know what Ruby thinks of these two different variables.
So if I say a.class, Ruby tells me that this is a fix note.
If I ask Ruby what type of class b is, it's a float,
and the difference between the two is going to be decimal places.
A does not have any decimals.
Now we can still add a and b together, and Ruby will give us something new.
In this case, it's 1 + 1.1, which is 2.1.
If we assign that to another variable and
then ask Ruby what kind of class that was, it will be a float.
Now I'm going to clear my screen here.
So we know that we can add different numbers together, and
we know that we have a string called name.
What happens if I try and add one of the numbers to a string?
It looks like Ruby gives us an error saying that there's no
implicit conversion of the class into another one.
Fixnum is the class of a.
And string is the class of name.
Now you'll notice that Ruby gave us a little message
saying that there was an error.
And it told us what the error was.
It's okay to make errors when programming, and in fact, it's quite common.
Go ahead and try playing around with strings and numbers in IRB now.
And don't worry if you make any errors.
You need to sign up for Treehouse in order to download course files.Sign up