The Basics of Programming / Arrays
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 are going to take a look at arrays in Ruby and again, this is a basic overview of arrays. So I am going to use RB to go ahead and play around with some arrays. Now, what an array is, it's a way to store multiple values in one type of container. So here, let's make a very simple array. We are going to call it "A" so we are using the variable "A" and it's going to contain one, two, three, four. So, there you go. It's a container that holds these values. Now to get those individual values, we would reference it by doing this. So here you have the name of the array and then we use the brackets to actually get to the individual elements. If I just put "A", that is the whole array. If I put "A" and then an element number, that is one element of that array. And in this case, we should get a one. You need to really pay attention that arrays, in general, in computers and programming languages, but arrays start at zero. So this is element zero, element one, element two, element three. I picked a bad array, I guess, to do that. Let's pick something else. Let's say, I'm going to reassign this and say A, B, C, D, E. Just so I'm not confusing you. I need to use, sorry, I should have - I need strings to do that. I wanted - there we go. So now we've got A and if I do A0, we get the 0. So, I'm just pointing out here that element 0 is the first element and there is many errors in programming because of this and they are off by one errors and that is people who forget when they are coding that arrays start at zero, they get like a count, if I do, again, everything in Ruby is an object, so this array has some, a bunch of methods assigned to it and one of them is the length and that will tell me how many elements are in there. So it's four. So for some reason I said, well, I've got four in here, I'm going to go through for, I'm going to look at each one of them and go one through four, so we did something like this, um, well, I can't really do it in here, but where you jump through and you manually put one through four. When you hit that four, this is what you are going to get. This is the error you are going to get. Nil, because there is no fourth element, you've got a third element, a second, a first and a zero element, but not a fourth. So you've got to make sure you keep that clear in your head what is going on with those arrays. Now, say we wanted to do something with each of those elements as we came across it. Well, this is where we get into blocks and I just wanted to show you a little bit what is happening with blocks and we will cover it again in another video, but essentially we can issue the statement "A" each and then we can go ahead and put in here and I will just use that and it doesn't stand for anything, it is just variables, so we've got these pipes and then variable. That can be anything that we wanted in there. Other than A because already used A, it's just a variable name. And I could say, put "S var". And all this is going to do is print out the individual elements of that array. Now, we could put anything in that statement, we can iterate through all those and figure out, you know, like looking for a particular value or anything along those lines to really make it powerful, about what we want to do when we are using arrays. And array in Ruby can contain different things. So I could say A0 equals, let's put on a whole thing here. Cat. Now if I go and show you A, it is changed to Cat. Now if I go to A, let's go to the second spot and it's going to equal a number. Say four. Now I'm going to print out A. So you can see that the array can hold different values, but again, everything in Ruby is an object, so you are not limited to storing just these kinds of things in array, you can actually have other objects in an array. Not to twist your mind too much, but let's say number equals twelve, so we have an object called number and if I go in here and say, class, fix num, so you can see it's an object, now I can see A0 equals number. Not pulled over the actual value of number and threw it in there, but there is a way to go ahead and append it on there, I'm not going to show you at the moment, because I don't want to get you confused, but there is a way to actually add things into an array that are objects. So that you can store actual objects in an array or a collection as it's also called. Collection is kind of more advanced array and we will see that in 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
United States 