Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Accessing Items in Arrays4:42 with Jason Seifer
Now that we've seen how to create arrays and add items to them, we're going to learn how to work with existing arrays and access what's inside them.
Index: The numbered position of an item in an array. Arrays all start with the first item having an index of 0.
Starting with the following array:
array = ["milk", "eggs", "bread", "ice cream", "pie", "potatoes"]
Access "milk", which is at the first position in the array (index 0), and assign it to a variable named "item":
item = array
Access the second item in the array (index 1):
Print out the first item in the array using the first method:
Print out the last item in the array, using the negative index and also the "last" method:
puts array[-1] puts array.last
The fetch method can be used with an index to return that item in an array:
puts array.fetch(2) # => "bread"
If a second argument is provided to the fetch method and there is no corresponding item in the array, the second argument will be used as a default:
puts array.fetch(20, "cake") # => "cake"
Return the number of items in an array:
Return the number of items in the array matching what you send in:
array.count("eggs") # => 1
To find out if an array contains a particular item, use the include? method with the argument of the desired item:
array.include?("eggs") # => true
So far, we've created arrays and added items to them.
What happens if we wanna get just certain pieces of data back out of the array?
We do that by asking the array for just a certain part of itself.
An item's place in an array is called the index.
And just to be complicated, the index starts with the number zero.
Let's say that we have our grocery list array with three items in it, milk, eggs,
Milk would be at index zero, eggs at index one, and bread at index two.
Once we know how indexes work, we have a lot of flexibility in how we ask for
things in an array.
Let's go ahead and see how that works now using WorkSpaces.
So here I am inside of a Ruby WorkSpace.
So let's go ahead and
bring the console up and practice working with arrays inside of irb.
Let's go ahead and create a grocery list array.
We're gonna say that that has milk, eggs, bread, ice cream,
pie, and potatoes.
Now when we look at this array,
we can see that it has six different items inside of it.
If wanted to access the first item inside of the array, we do that
by typing the name of the array and then an open bracket and the index.
Now remember, indexes start from zero, so
if we want the first item, we have to type the number zero, which returns milk.
Likewise, if we wanted the second item, we would use an open bracket, type
the number one, and then a close bracket, and that will return the second item.
Now in addition to using brackets, we can also use the method
at and then pass that the argument of the index.
And it will return the item at that index inside of the array.
So we could say item is the grocery list at index one.
And now our item will still have the grocery list item at index one.
And if we look at our grocery list, it still contains all of the different items.
Now I'm going to clear my screen here, and let's just look at our grocery list so
we know what we're looking at.
There's some special methods for accessing the first and
last items in the array, and those methods are first and last.
Another way of accessing the last item in the array,
is typing out the variable name [-1].
Which goes in the reverse order of the index.
We can also use -2 and so on.
So I just cleared my screen here, and now that we know how to access different ideas
in the array we can do cool things like insert items at a given position.
For example, if I wanted to insert oatmeal into this array and
I wanted to insert it after bread, I could say grocery_list.insert,
give it the index and then, the item to insert there.
And now, we've added oatmeal into the grocery list array at the index of two.
We could also ask our grocery list how many items are inside of it.
By using the length method.
An alias for that is count.
We can do something else kind of cool by asking for
the count, of a certain item in the array.
So if we want to see how many times eggs appears inside of this array.
We get that one time.
So if we add it in there twice, you'll see it appears in there twice.
And then if we ask how many times eggs appears in this array,
we get the number 2.
We could also use the method, include, to see if something is included in the array.
So does this array include the string eggs, and it does.
But if we ask if it includes water It does not.
Try working with array indexes now on your own using WorkSpaces.
You need to sign up for Treehouse in order to download course files.Sign up