From the course: Complete Guide to Ruby

Array methods

- [Instructor] We learned the fundamentals of creating and working with arrays. We will use arrays frequently, and Ruby provides many methods to manipulate them. Let's go straight to IRB and try out some of the most common methods. I'll go into IRB and I'll create an array that we can use for demonstration. Inside that array I'll put 2, 4, 4 nil a, and then another array that has b and c, and then I'll end that array and end the outer array. It's often useful to know how many objects are inside an array. We can do that with array.length or array.size. They're synonyms. It returned six. The last object is an array that happens to contain two more objects, but it only gets counted as a single object. We can use the reverse method in order to reverse the order of the objects in an array. It's returning a new array to us, but it's not changing the original array. Some Ruby methods have an alternate version that has an exclamation point at the end. Often it indicates that something is destructive or more powerful than the basic version. If we use reverse with the exclamation point, then it updates the array with the reversed list. We can use the shuffle method and it will mix up the order of the objects. If I call it more than once, you can see that every time it gives me back a new array that's been shuffled differently. Again, it's not permanent, but I can use the exclamation point version and it will update the array with the new order. Let's clear the screen and notice that our array has two 4's that are in it. We can use array.uniq, short for unique in order to remove the duplicates. It also has an exclamation point version that will update the array at the same time. Notice that our array has a nil in it. We can use array.compact and it will remove all the empty positions in the array. It also has an exclamation point version that updates the original array at the same time. Notice that our array has an array inside of the array. That's that b and c that's here. We can use the flatten method and it will take those objects out of their inner arrays and make them all objects of the outer array. Again, it has an exclamation point version that updates the original array with the results. We can query an array to find out if an object is inside of it. So we can have array.include? and then in parentheses, we can provide an argument to say, does this include the in integer two? Of course, the answer is true. If I were to ask whether that includes the in integer one, it says false. It's very common in Ruby to have methods that return true or false end in a question mark like include does. We can remove items from the array. We can remove them either by using the position, delete at position one. Now that will remove a. You see it returned a back to us, but it also removed it from the array. It didn't preserve the space where the a used to be. It deleted it and shifted the other objects over. If I do array.delete, and then c, I can tell it the object that I want to delete from the array. So it returns back c to us because it found it and it also updated the array. All right, let's clear the screen. And let's start with an array that's 1, 2, 3, and 4. I can call the join method on an array, and it will join all of the objects together as a single string, so it converts an array to a string by joining objects together. It's useful to provide a delimiter as an argument like comma. And now you can see that my string has commas between the items, but not after the last item, which is nice. And we can change what that string is. So I can do space dash space, and it'll join it together with those. The opposite of join would be split, and that's a string function. So let's say we had the string 1, 2, 3, and 4. We can call split on it and tell it to split on the commas. That will then gimme back an array that has been split up based on those commas. That's handy too. Now let's go back to our original list. Let's have numbers equals, and we'll have 1, 2, 3, and 4. Now if we say numbers.first, it gives us back the first item. It doesn't actually change the array, it just tells us what the first item is or numbers.last does the same thing. So we can find out what is the object at the beginning or the end. If we want to actually change the objects, we can push items onto the end using push. So numbers.push. Let's push the number five onto the end. Now, if I type numbers, you can see it's been pushed to the end. This is what we call stack behavior, and we can actually pull the last item off of the numbers by using pop. So that'll pop it back off. You see it gives me back the number five and assigns it to the variable last. If we look at the original one, you can see that it's been removed from there. So we can push items onto the back and pop them back off. We can do the same thing to the front end if we used first equals numbers.shift, it'll shift off the first one, and we can do the opposite of that, which is numbers.unshift and we'll put back what we captured inside the variable first. So now it returned one back to the front, so push and pop for the end, shift and unshift for the front. And finally, I want to show you that you can actually add together arrays and combine them by using the plus operator. So 3, 4, 5. You can see that it just combined the arrays together, or I can use subtraction. Let's say that I have the array 1, 2, 3, and I want to subtract an array two. Well, it knows what the difference of that is. It's one and three. Or I could subtract two and three and it would remove both of those. Or one and three would give us back two. So we've seen many array methods. We saw length and size, reverse, shuffle, unique, compact. We have flatten. We saw include, we saw delete, objects either by their integer position or by the object itself. We saw how to use join as well as the string method split. And then first, last, push, pop, shift, unshift. Spend some time playing with arrays and trying out these different manipulation methods.

Contents