Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Ruby
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!


  • 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.

  • Intro 0:00
  • Arrays 0:12
  • Creating an Array with a Block 0:50
  • Alternative Ways to Create an Array 3:52
  • Checking the Class 5:14
    • Iterate through the array
    • Call the class method
  • Array Shortcuts 6:38
    • at(index)
    • delete_at(index)
    • first(n)
    • last(n)
  • Removing Duplicates 9:58
    • uniq or uniq!
  • Sorting the Array 10:48
    • sort or sort!
  • Getting the Index 11:35
    • index
    • rindex
  • Multidimensional Arrays 12:56
    • flatten
  • Check Out the Earlier Lesson 14:16
    • Ruby Data Types Part 2

Transcription: Arrays

Welcome back to

Today's lesson is on arrays.0002

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 can also hold a String...multiple types in one piece.0036

Let's look at creating an array with a block.0049

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; I'm trying to get all the odd values, so I'm calling this 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

It's going to do that for the size of 10.0172

So, we have 1,3,5,7,9,11, all the way to 19, and we have our odd values.0177

Remember that this can put what you want to put there, so if you are going to instantiate a new object to fill in this array, you just pass in the constructor to that.0188

Again, I have my odd values here from this last example.0202

But, notice, I call float with that x value.0207

What this is going to do is, it's going to return me that array again, but in Float type.0212

So, I'm going to get 1.0, 3.00, and that is all going to go to 19.00.0217

In this next part, we're going to look at alternative ways to create an array.0232

The first one--I've rarely seen this before--you call array, put a period, and then you put your square brackets, and then you pass these parentheses with your elements.0242

When you do that, it's going to create you this array right here.0257

Now, the returns for these three different ways to create an array are all equal.0260

In this second part, we are using the Kernel; it's going to call array with the square brackets--[abcde]--oops, I meant to put an f here, too...0270

Then, it will return [abcdef].0287

This last one is probably the easiest and the most popular way.0292

You will be using it, and you will notice it makes the exact same array as these ones up here, but I didn't have to call array.0297

Just take the literal array for that.0308

Next, we are going to look at checking the classes in the array.0314

We are going to go over iteration and class types.0321

This will iterate through the array--we're going to iterate through the array--and then we're going to call the class method.0325

We're actually calling the class method on the objects in it.0332

If I have this array object--here is my array object--this could be anything defined in here--and I call .each, it's going to give me each element in that array.0337

I'm going to print out that class.0356

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 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, 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

But, that array, it will update it itself.0472

Otherwise, it returns nil, if the index is out of range.0477

If it's out of range, it is going to just return nil; it's not going to delete anything.0481

Here I have our example, a=[abcde]; I'm going to delete_at index 1; it's going to remove this b; and then, I'm going to get [acde].0487

Next, we are going to look at some array shortcuts.0507

The first one is this first method.0512

I pass in n: what this does is, it returns the first element--so n here is optional.0515

I can just pass in first, and it will give me the first element in the array.0532

Now, what happens is, if n is passed in, it returns the first n elements of the array.0536

Notice here, for my example: I call a.first--it returns a; but if I call first with 3, it's going to get me the first n elements.0545

My n is equal to 3 here, so it's going to get me [a,b,c]--and notice, it also creates an array for that.0556

Again, we can do the same thing with last: this is another method--it returns the last method, and it is optional if n is passed.0568

It returns the last n elements of the array.0577

So, when I call a.last, it gives me e, but if I do 3, it gives me the last n elements--in this case, n=3, so [cde].0580

Now, let's look at removing duplicates.0596

I use a method called uniq here-U-N-I-Q; you can also call it on itself with a bang.0602

This removes the duplicate values; no changes if no duplicates are found.0609

For example, we have this string--it's a basket full of fruits: Apple, banana, apple, grapes, pear, banana.0618

Notice, we have a couple of duplicates--banana, apple--and I'm going to call uniq!, so it's going to update itself.0627

Now we have no more duplicates: when I call basket again, it's going to have apple, banana, grapes, and pear.0640

Let's look at sorting the array with the sort method--there is sort and sort!.0650

This sorts the elements using this less than, equal to, greater than operator.0657

So, if I have a group of numbers that are unsorted right now, I call the sort method, and now it's going to order them from lowest to highest into [5,6,7,9]--this is after I call Numbers.0665

This .sort! will sort it and update right on that line.0683

Now, we're going to look at getting the index.0694

There is also a method called index, and it returns the index of the first object it sees with that.0699

So, you call index, and then you pass in the element that you want it to find.0710

For this example, I have a=[abbcd].0721

I'm looking for the element b; it tells me the index is 1, because that is the first one it found.0727

Even though there are two b's here, it is going to go from the 0 place and move down the ranks.0734

It's going to transverse like this, all the way down, to find it; and since it found it here, it's going to stop, and so it gets the value 1.0743

But, we can also do the rindex; that does the reverse--it starts from the back of the array, to the front.0753

Here, I call rindex(b); it's going to start from d, and notice, it finds it on the second place--the second index value.0763

Next, we are going to look at multidimensional arrays: an array that holds an array of arrays.0776

This is can hold arrays in arrays, too--so that is what a multidimensional array is.0783

For our example here, we have Numbers--we have an array, and then we have our level 2 array right here, and that is our array in an array.0791

The reason I want to bring up this multidimensional array is, now we are going to look at this method called flatten.0806

What flatten does is, it returns an array that is a one-dimensional flattening of itself.0812

So, when you call flatten, it will update itself with that.0819

It flattens recursively, so if I have an array of an array in an array, it's going to flatten the whole thing.0822

Let's take this example with Numbers; I call Numbers.flatten, and this is what I'm going to get. It's going to just be one level of array, [1,2,3,4,5].0832

Other than that, that is the end of the array lesson.0856

Be sure to go back and check out the Ruby Datatypes Part 2, where we have more information on arrays.0861

That is it, so thank you for watching, and see you at the next lesson!0871