Lesson content is currently in draft form.
In the last chapter, we dealt with the Twitter user data object, which when parsed, is a Ruby Hash
.
But the data object for a page of tweets is different:
1 2 3 4 5 6 7 8 9 10 |
|
If you look at the contents of the JSON in tweets_obj
, you should see something that looks like a collection of Hash
objects. But what exactly is this Array
that holds them?
The Array object
In Python, arrays are known as lists, which may be the easier way to think of this type of collection. Like the Hash
, the Array
is a collection of objects. However, instead of using keys, the objects in Array
can be accessed (using the same kind of square-bracket notation) in a numerically sequential order.
Here’s a sample Array
:
1 2 3 4 5 6 |
|
We’ll examine arrays in greater detail later. For now, note:
Array items are accessed with integers. They don’t use strings or other types of data-objects (as a
Hash
can)A Ruby array is zero-indexed. That means the first element is found at the address of
0
. The second element is at1
, and so forth.
Collections inside collections
The string and number datatypes are primitive compared to the collection datatypes. As an example, the tweets_obj
array contains a bunch of objects, and if you inspect each object, you’ll see that each is itself a Hash
:
1 2 3 4 5 6 7 |
|
In other words, each individual Tweet is represented as a Hash
Exercise
Look over the downloaded JSON to see the structure of the Tweet hash object. Print out the following:
- The text of the first tweet
- The number of retweets of the eighth tweet
- The date of the 14th tweet
Answer
1 2 3 |
|
Bonus: What happens when you try to access an address outside of an Array’s range? Use the length
method to find the number of items in an array.