2013-12-21 59 views
0

我有這樣的代碼:在for循環中展望未來,儘管錯誤

require 'octokit' 
require 'csv' 

client = Octokit::Client.new :login => 'github_username', :password => 'github_password' 
repo = 'rubinius/rubinius' 
numbers = CSV.read('/Users/Name/Downloads/numbers.csv').flatten 
# at this point, essentially numbers = [642, 630, 623, 643, 626] 

CSV.open('results.csv', 'w') do |csv| 
    for number in numbers 
     begin 
      pull = client.pull_request(repo, number) 
      csv << [pull.number, pull.additions, pull.deletions] 
     rescue 
      next 
     end 
    end  
    end 

然而,在次client.pull_request遇到404,然後躍過並進入下一個。但是,它仍然需要打印的數量numbers數組中,然後把一張空白或零pull.additionspull.deletions,然後在陣列中移動到下一個項目,從而產生類似:

pull.number pull.additions pull.deletions 
642,  12,   3 
630,   , 
623,  15,   23 
... 

如何這可以做到嗎?

回答

1

我已刪除了for循環,因爲它不存在於自然界rubyish,下面應該工作

require 'octokit' 
require 'csv' 

client = Octokit::Client.new :login => 'github_username', :password => 'github_password' 
repo = 'rubinius/rubinius' 
numbers = CSV.read('/Users/Name/Downloads/numbers.csv').flatten 
# at this point, essentially numbers = [642, 630, 623, 643, 626] 

CSV.open('results.csv', 'w') do |csv| 
    numbers.each do |number| 
     begin 
      pull = client.pull_request(repo, number) 
      csv << [pull.number, pull.additions, pull.deletions] 
     rescue 
      csv << [0,0,0] 
      next 
     end 
    end  
    end