Join Kevin Skoglund for an in-depth discussion in this video Integers, part of Ruby Essential Training.
In this movie we are going to take a look at the Ruby object type integers. Now, integers are just simply numbers, and numbers are mostly commonsense. But in Ruby, numbers are actually divided into two major categories. We have integers and then we have floating-point numbers, which are called floats for short, and they are better known outside of programming as being decimal numbers. Ruby is going to separate them out into two categories. Let's take a look at integers now. I am going to open up irb. We were working with integers before when we had 1+1 or we had x=2.
Those are integers, and we are doing assignment with integers. It's just a basic number. That's what an integer is. We saw that we can do other operations. For example, we could have 4/2, we could have 4*2, we can have 4-2, those will all work for us. There's also an exponential operator, two asterisks together, and the result is 16. That's four to the second power. We also saw how we could do assignment, x=4. We can do an operator with assignment, plus equals 2, and notice what that gives us back.
It gives us the result of the operation, 4+2. which is 6, but it also changed x at the same time. So that now x is going to be equal to the result of it. So it both incremented it and made the assignment. It's the same thing as if we had typed x=x+2. It does the exact same thing. It's just a shorthand to do it this way. We can do the same kind of assignment operator with all of these other things too, the division, multiplication, minus, all of those work the exact same way.
Just put them in front of the equal sign and it will both perform the operation and do the assignment at the same time. It's kind of a nice feature. We can also use parentheses in Ruby, (1+2) *3. You see that it comes back with 9. So integers work pretty much the way we would expect them to work. Now, let's take a look at something else. We said that integers are objects. So let's do .class, and that will tell us what class it belongs to. Before I explain this, let's try another one, 123456789123456789.class.
Now, notice that when I did one of them, its class was Fixnum and the other one was Bignum. Neither one was class integer, which is what we might have expected. It's because integers actually belong to one of two subclasses; they are either put into Fixnum or Bignum, both are subclasses of integer. The difference is only in the way that Ruby stores these values in memory. It uses more memory to store these bigger numbers, so it's going to reserve a bigger amount of space, but to keep it from taking up too much space all the time, it also has the smaller more efficient structure called Fixnum that it can use for smaller numbers.
So we have Fixnum and Bignum and Ruby will switch back and forth as needed and so you will never need to worry about the difference. Just think of both of them as being an integer. You can try this out with a couple of things. If you want to multiply together two Fixnums, you can see how long it takes for you to get to be a Bignum. So for example, let's say x=1234* 1234*1234, and than x.class, Bignum. So you see it just switched back and forth between this and there wasn't a problem.
Integers can also be negative, so -200 is a perfectly valid integer. 200.abs is the absolute value. That will return the absolute value by applying the absolute value method, which we used in the dot notation we have seen before. We can also have 200.next. That's another nice method that we can apply to integers and it will return the next integer that comes after it. It's the same as if we would have said 200+1. That's really all there is to integers. Just don't be thrown by the Fixnum, Bignum distinction. If you look at the class, they are both considered integers.
But if you keep that in mind, I think integers will be a breeze. But in order to really understand the way that Ruby handles numbers, we need to not just look at the integer class, we also need to look at the float class, and we will do that in the next movie.
- Using Ruby in the Interactive Ruby Shell and in standalone scripts
- Learning to write custom code blocks to find, merge, and sort
- Using modules for namespacing or as mix-ins
- Reading from and writing to files
- Creating a full Ruby project from start to finish