Lesson content is currently in draft form.
Sometimes, we just want one value from a collection, just like when we wanted the Twitter account names from each line in the Congressmembers’ datafile. To make an array of these values, we can use each
to fill a new array:
1 2 3 4 5 6 7 8 |
|
Transforming an array
Creating an array of values plucked out of a collection is such a common pattern that Ruby has the map
method (also known as collect
)
1 2 3 4 5 6 |
|
The do..end
block can also be expressed with curly braces
1 2 3 4 |
|
Think of the code inside these blocks as methods. Like methods, the last line is the return value:
1 2 3 |
|
`
1 2 3 4 5 6 7 8 |
|
Sort an array
(draft)
1
|
|
1
|
|
The sort method
The sort
method also accepts a block, for cases when a collection’s data objects need custom sorting methods:
1
|
|
Exercise
Sort the congressmembers by highest tweet rate.