我有这个代码:
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.additions
和 pull.deletions
放置一个空白或零,然后移动到数组中的下一项,从而生成如下内容:
pull.number pull.additions pull.deletions
642, 12, 3
630, ,
623, 15, 23
...
如何做到这一点?
我已经删除了 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|
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
您是否尝试过使用开始/救援/确保,以便救援/确保代码将适当地设置拉变量?有关示例,请参阅 https://stackoverflow.com/a/2192010/832648。