尽管有错误,仍然在for循环中前进

时间:2013-12-21 19:11:34

标签: ruby octokit

我有这段代码:

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
...

如何做到这一点?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您是否尝试使用开始/救援/确保救援/确保代码将适当地设置拉变量?有关示例,请参阅https://stackoverflow.com/a/2192010/832648