Learn Why to Code

A brief introduction to practical programming

« Previous: Reading from a spreadsheet-like file Next: Filter a collection »

Plucking Out Values

The problem: How can easily collect just one attribute from a collection of Twitter account info or tweets?
The solution: Use collection methods to transform collections to a particular value

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
load './my_code.rb'

cmember_data = get_congress_data_from_file( CSV_DATA_MAIN_FILENAME )
cmember_twitter_names = []

cmember_data.each do |c|
  cmember_twitter_names << c['twitter_id']
end    

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
load './my_code.rb'

cmember_data = get_congress_data_from_file( CSV_DATA_MAIN_FILENAME )
cmember_twitter_names = cmember_data.map do |c|
  c['twitter_id']
end

The do..end block can also be expressed with curly braces

1
2
3
4
load './my_code.rb'

cmember_data = get_congress_data_from_file( CSV_DATA_MAIN_FILENAME )
cmember_twitter_names = cmember_data.map{ |c| c['twitter_id'] }

Think of the code inside these blocks as methods. Like methods, the last line is the return value:

1
2
3
arr = ["cat", "dog", "mouse"]
new_arr = arr.map{|a| a.upcase}
#=> ["CAT", "DOG", "MOUSE"]

`

1
2
3
4
5
6
7
8
arr = [1, 2, 3]

arr.map do |a|
  b = a + 1
  c = b * 2
end

#=> [4, 6, 8]

Sort an array

(draft)

1
[1, 6, -3, 100, -200].sort
1
cmember_data.map{ |c| c['lastname'] }.sort

The sort method

The sort method also accepts a block, for cases when a collection’s data objects need custom sorting methods:

1
cmember_data.sort{|c| c['lastname']}

Exercise

Sort the congressmembers by highest tweet rate.

Comments