Converting a clunky ruby function into a foreach/loop?

86 views Asked by At

I have this code:

require '/octokit'
require '/csv'
CSV.open("node_attributes.csv", "wb") do |csv|
  csv << [Octokit.user "rubinius"]
  csv << [Octokit.user "sferik"]
  # add one more request for each piece of information needed
end

However, it would be smoother if I could have it iterate through a list and call the Octokit.user command for every name on the list, e.g.

list = ["rubinius, "sferik"]

How can I convert my clunky function into a nice iterator that goes through the list?

2

There are 2 answers

0
Max On

Inside your block do this:

list.each {|item| csv << [Octokit.user item]}
0
Linuxios On

Try this:

require '/octokit'
require '/csv'
CSV.open("node_attributes.csv", "wb") do |csv|
  users = ["rubinius", "sferik"]
  users.each do |u|
    csv << [Octokit.user(u)]
  end
end