In this lesson, our instructor talks about arrays. He discusses creating an array with a block, alternative ways to created an array, and checking the class. Then he talks about array shortcuts, removing duplicates, and sorting the array. Lastly, he talks about getting the index, multidimensional arrays, and earlier lesson.
Arrays are ordered, integer-indexed collections of any object
Arrays can hold objects of different types
To create an array with a block, it requires a parameter to instantiate the number of elements and a code block to set the values.
A multidimensional array holds an array of arrays.
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.
I know we have gone through some arrays with Ruby Datatypes Part 1, if you had it--be sure to review that--but this gives you a little scope on some more methods, and ways you can manipulate and change arrays and create arrays and delete them.0008
What are arrays? They are ordered, integer-indexed collections of objects.0027
They hold objects of different types; so in one array, it can hold an Integer...it can also hold a String...multiple types in one piece.0036
We already went over, in a past lesson, to just do .new with the size, but this time we're actually going to initialize it with this block.0055
What this does is, I can actually customize this block to tell it, whatever it returns at the last piece of information, that is what is going to initialize as the object, and it uses this index value.0067
To do this, it requires the parameter size to instantiate the number of elements; the size is an Integer value.0081
It requires a code block to define the value to set.0097
This block starts with the value 0 to n-1; this actually means the index.0104
Starts with 0 to n-1, which is this index value...where n is the size.0117
For this example, we have this array.new; I'm trying to get all the odd values, so I'm calling this array.new: it has a size of 10, and here is the index that it starts with--that is 0 to 9.0128
First, it's going to do 0 times 2, which is 0, plus 1, and that's 1--it's here.0148
Next, it's going to start the value 1--so that is 1 times 2 plus 1, equals 3.0162
When I do that for A, B, and C, and then I call .each, it's going to print me String, String, String--which is the class for it.0358
If I had a different...an Integer, it would print out Integer there, too.0373
That way, I can know all the different classes that are being used in the array.0376
When I put in my own code, I'll probably not just print it out; I will probably look at these classes and do something with it--manipulate it or store it somewhere else, depending on the class--or convert it to a different class.0381
The next one we are going to look at is array shortcuts.0397
Here you have this method at, and it has the index you want to look at.0401
When I call the at method with that index, it is going to return me the element at that index.0410
Here is an example: I have a = [a,b,c,d,e]; for the index values, 0,1,2,3,4.0418
So, if I call a.at(0), it's going to return me the element a.0431
Ask for 2: it returns me c; ask for 4: it returns me e.0438
Now that you know the at method, you can move on to the delete_at method, which will probably be equally useful, I assume.0445
Same parameter: you pass the index; what it will do is, it will remove the element at the specified index.0455
It returns the element deleted; so it is not going to return you the array after you delete; it's going to just return you that element that it just deleted.0463