0

What exactly does CSV do? (I know its a gem)

What is SLICE_SIZE? (Line 13)

Why is csv insided straight braces?(Line 16) CSV do |csv|

Can You explain the whole lines 17, 18, 19?I am completely lost on that?

-----------------Code--------------------------------------------------

    require 'twitter'
    require 'csv'
    def twitter_client

       @twitter_client ||= Twitter::REST::Client.new do |config|
            config.consumer_key = ""
            config.consumer_secret = ""
            config.access_token = ""
            config.access_token_secret = ""
       end
   end

   SLICE_SIZE = 100

   def fetch_all_friends(twitter_username)
       CSV do|csv|
           twitter_client.follower_ids(twitter_username).each_slice
           (SLICE_SIZE).with_index do |slice, i| 
                 twitter_client.users(slice).each_with_index do |f, j|
                       csv << [i * SLICE_SIZE + j + 1, f.name,
                       f.screen_name]
                 end
           end
       end
   end
Sharath Zotis
  • 167
  • 1
  • 2
  • 7
  • I'm afraid this is a little too broad: A good answer would need to explain too many things about Ruby. Would you consider reading a basic Ruby tutorial? Especially, learn about constants, blocks, and enumerators. When you have a specific question, please feel free to ask it here. – Wayne Conrad Jul 13 '15 at 18:45

2 Answers2

2

CSV is a class, implementing handling of CSV data, as follows from its documentation.

each_slice is a method of Enumerable which takes only so many elements from source collection per each iteration. This is done to reduce memory requirements of the computation or, possibly, to delay fetching more data until current chunk is processed, for example. The SLICE_SIZE value is how many elements to take.

CSV do |csv| initializes a csv object and passes it down to the block as a parameter. This is a way to organize code so that e.g. initialization is separate from business logic of the block. The block is delimited by do and end keywords.

The next two lines are actually a single statement:

twitter_client.follower_ids(twitter_username).each_slice(SLICE_SIZE).with_index do |slice, i|

It takes a collection of follower ids of twitter_username from the Twitter API, takes elements from there in chunks of SLICE_SIZE and passes each chunk and its index into another block. Contents of this block are executed as many times as there are chunks of SLICE_SIZE in the follower_ids.

The next line

twitter_client.users(slice).each_with_index do |f, j|

works only on the element of current chunk. It takes each element of the chunk and passes it along with its index within the chunk to yet another block.

Up to this point it's more of collection handling than actual business logic.

The most inner statement

csv << [i * SLICE_SIZE + j + 1, f.name, f.screen_name]

creates an array of three fields: an index of some sort, the Twitter-supplied name and screen_name. This array represents a CSV line. The << operator pushes that array into the csv object which was mentioned before. The csv object adds it to what it has collected by this time.

When this code finishes you will have a csv object filled with data received from the Twitter API and ready to be saved to disk.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Nic Nilov
  • 5,056
  • 2
  • 22
  • 37
0

There used to be a gem faster_csv that you needed to call but not anymore. You just require the CSV library.

It is usually used to either read or create CSV files.

It is a bit hard to answer your question when there are no line numbers. You can learn more about Ruby's CSV library at: http://ruby-doc.org/stdlib-2.2.2/libdoc/csv/rdoc/CSV.html

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Elle
  • 236
  • 3
  • 5
  • [Faster_CSV still exists](https://github.com/JEG2/faster_csv). It's the default for Ruby now. See http://stackoverflow.com/q/5011395/128421 for more information. If running on an older version of Ruby that doesn't use it then use the gem to add that functionality. – the Tin Man Jul 13 '15 at 17:48