Home
Username:
Password:
Programming With Ruby Tutorials

Symbols & Ranges / Ranges




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

In this video we're going to look at ranges in Ruby. Now, a range is actually pretty easy to understand. Essentially it's just a, a type of construct, a variable, if you will, that is inclusive or exclusive of a set of numbers. So if I do that and then we look at our one, oops, I guess I got to be in IRB to go ahead and do that. So we look at our one. If I look at the class it's going to tell me it's a range and the construct for that, how you actually build it is three, two periods and six. Well, what does that get us? Well, it actually represents the numbers from three to six. So if I want to look across on each of those, I can say our one each and then let's just do a simple block here and I'll go X put X. Put, there we go. So you can see it just prints out three through six. But as you get into more advanced programming problems, having access to a, a variable that can hold a range like this is a very powerful tool because you can create ranges on the fly and you can't really put variables necessarily in here, but you can create them and utilize them within your program like I showed you right here with a simple iterator. You can use them to create arrays if we, go in here and say, uh, two A, you'll get an array. It's a very quick and easy way to create an array if you need to on the fly kind of thing. Or you can also test for a range membership if you want by actually doing a, uh, include. And let's just say ten. We'll say false. Now let's put five is true. So you're testing whether it's going to be in the range or not. Now, be careful in using num, you know, integers, fixed num, if you will. Keep your verses a string. So you might deceive yourself into thinking that this is a valid range and it might appear so, but let's take a look here. It didn't like it for one thing to come in that way. Let's go up here and do each and run it into our little block and see if we can get something out there. Well, see, it's just looking at it like it's some kind of string type of construct. So you got to be really careful and make sure you're using numbers. The other one that's going to fail is if you say two point zero and two point five. Then if we go up there and, uh, two A on it, it's not going to work because, think about this, in a real number like that, well, what do you want it to do? Do you want it to go two point zero, two point one, two point two? Or do you want it to go two point zero one, two point zero two, all the way up to two point one and then two point? See, it isn't, it doesn't know how to decide here and it's infinite. Absolutely infinite number of ways to actually do that. So really, ranges you should stick around when you're just creating them from numbers like this, from the, from basic integers like I've done right here. Now, you can create your own custom ranges. Uh, it's not entirely hard to do but I'm not going to actually demonstrate it but you can do it by declaring essentially a class and as a subclass of the range and then going ahead and utilize that in the same manner with this. I don't know how much you're going to gain by doing that, but I do know that for just doing number ranges and checking for things inside that range, it's a very powerful, uh, little tool to have access to. Now, the other part of it is that you can have the what I showed you here was, let's do the two array, ok, I need to assign this to a variable real quick here. Now, if I do, well, let's look at our R2 array. But now, if I change this, I'm going to go up here back to our two and I'm going to add an extra period in here. Now let's do R2 to A. So you get this exclusive thing going on where it knocks off that end and you only have the first part of that array. So that's using an exclusive array and that's done by putting in the three dots like that and that's useful in some circumstances, especially if you need to deal with the off-by-one type of error. So that's a little bit about ranges and you'll be seeing those more in some programs coming up and other videos.

Tutorial Information

Course: Programming With Ruby
Author: Al Anderson
SKU: 33788
ISBN: 1-934743-01-1
Release Date: 2007-08-21
Duration: 8.5 hrs / 113 lessons
Work Files: Yes
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available