Intro to Ranges in Ruby

Boris Paskhaver
A free video tutorial from Boris Paskhaver
Software Engineer | Consultant | Author
4.7 instructor rating • 6 courses • 292,865 students

Lecture description

  • Introduce the Ruby Range object for storing an interval or sequence of numbers / characters
  • Call the .class.first, and .last methods on a Range object

Learn more from the full course

Learn to Code with Ruby

A comprehensive introduction to coding with the Ruby programming language. Complete beginners welcome!

31:25:46 of on-demand video • Updated September 2020

  • Learn to program in the Ruby programming language
  • Master common Ruby objects like Strings, Arrays, Hashes, and more
  • Solve popular coding challenges and algorithms with Ruby
  • Dive into object-oriented programming including classes and more
English [Auto] All right. And this quick section will be exploring a brand new Ruby object called a range a range stores a sequence of numbers or letters. So another good word for describing rage is an interval. It starts from a point a and it includes all the values from that position all the way to an ending position point B so to speak. So for example we can have a range of numbers that goes between 1 and 5 and that will include the numbers 1 2 3 4 and 5 and that range object captures the entire interval that can be particularly helpful. We need to store something like a thousand numbers in sequence. We don't have to write them all out. Rather we have this range object to represent that collection. So let's dive right into it and create our very first Range object. I'm going to store it in a variable called numbers. So it's do and you have mass equals. And here is the syntax for a range. There's actually two options of going to begin with one will dive into the other one in just a moment. I'm going to write the beginning of my range. Let's say I want to start this one at 1 and then going to place two dots or two periods and then I'm going straight the end point of my range. So let's say I want to end this one at five. All right. Now when I output numbers with something like P You can see that we're actually going to get the exact same thing that we type that's not visually apparent us that this is storing all of the numbers in between 1 and 5 and even if I do print here or puts you can see the result is going to be the exact same. So of course print is not creating the line break. Let me switch that around here. Here we go. You can see that either one of these outputs isn't necessarily telling us visually what this includes But rest assured this range object is containing all of the numbers from that beginning to the end. If I call the class method on my rage object member of the class method is available on all objects in Ruby. It returns the class from which the object is built and the class is just the blueprint. That's the thing that enables that object to have specific methods and functionalities. We can see that this is in fact a rage object. Now as I mentioned there's actually two options when we're creating ranges. And the second option here is to include three dots instead of two like so. So what three dots does is exclude the final value of the range surviver range like this. What this means is five is now going to be excluded. If I do three dots so this collection this range is going to include numbers 1 2 3 and 4. It's going to end at 4. However if I take out that last dot and it's only to the last number or the last sequence of the interval is going to be included. So with two dots it's going to represent 1 2 3 4 and 5. So two dots includes the final value three dots excludes the final value. Otherwise the range will include all the values in between the start and end values. To close this lesson off let's take a look at two methods that are available on ranges. First and last. Now this is a kind of an interesting collection of methods because they can take an optional parameter so they can be used with or without arguments. And I recommend in this case that you always give an argument and I'll show you why in just a second. Let me just clear this output right here. Let's go ahead and call both methods at the same time. I'm going to do Nom's first and does that last but this is going to do predictively is return the very first element of that range the beginning of the interval. And the last method is going to return the last position of the interval. So predictively for a range of 1 and 5 are going to get 1 in 5. That's the beginning and the end. If I change this to something like 92 a hundred 150 because our range could represent any sequence or any interval we can see that the beginning here is 90 and the end is 150. The reason why I don't like these matters by themselves is when we include that third dot the last method is still going to show us the the number that's technically being excluded. So if I execute this we're still going to get 90 and 150 even though 150 is technically not included within that range object. This isn't a bug. This is as has a regular design feature of Ruby because technically speaking somewhere behind the scenes it's going up to 150. It's just not including it. So 150 is stored as the last position or the last end point is just not specifically included but of course to us as humans it is a little bit bizarre and a little bit weird because we might ask something for the last number in the range and it's not actually going to be 150 in this case it's 149. So in order to control for that what we can do is give the the art the methods here their own custom arguments. So I'm going to open my parentheses here to provide these arguments right here. Let's do it one at a time. And what we give to the parentheses as an argument is the number of elements that we want to return. So in this case if I'm calling the first method this is going to represent the number of elements that I want from the very beginning of the range from the very beginning of our interval. So if I were the very first four numbers I'm going to put four in here and I'm going to get 90 91 92 and 93. They'll notice that these numbers are separated by commas and stored within this box if you will that's surrounded by square brackets. This is actually called an array object that will be diving into it in a future section but you can basically think of an array very simply right now is just a list or a collection of items. So this is just telling us that we have four values here 1991 92 93 and that makes sense. Similarly if I do something like last and let me just remove this this dot right here. So our 50 is included. Now we're going to get 147 148 149 150 It's giving me the last four numbers that are in my range interval right here from 90 to 150. Now as I mentioned when you're adding three dots and the last number is Xclusive not included. If we do something like last unfortunately it's going to tell us that we have won 50 which isn't really accurate. Well we can do here. When we open our parentheses to give the argument is simply give it one that tells us we want the last element the last position the last thing included within my numbers range at this time we're actually properly going to get 149. Granted it is given to us in an array. We can see that by the square brackets it's basically a list that includes just one item on it. You can see we're probably getting 1:49. Let me in fact offer you a side by side comparison. If I do last without an argument and last with an argument you can see we're getting 150 versus an array containing the value 149 right here. This is more accurate. OK. So the last method with an argument will take care of that slightly confusing aspect of the range design and we'll show you the actual last component. You can see here if I remove the third dot and make 150 inclusive Now this execution live before is also going to return 150 this time in an array because this time 150 is included. OK. So that's basically a very quick introduction to numeric ranges. It's just a Ruby object that represents a sequence or an interval. It can include a numbers or letters. We're going to look into alphabetical ranges in the very next lesson. And the other key thing here is remembering that two dots means that the last value is going to be included three dots means it's going to be excluded and we could use the first and last methods to find out where the range begins and where it ends. If you provide arguments which is what I recommend. Make sure it's a numeric number and integer and that's simply going to extract that number of elements from the beginning or the end of the range. OK. Saucier in the next lesson.