我的代码:
feeds = Website.find(:all).map{|w| w.feed_url}
feeds.each do |u|
begin
open(u)
rescue
feeds.delete(u)
end
end
示例Feed可以是:['http://da.wordpress.org/feed/', 'asdasd']
如果有任何错误,我想解决任何错误并从Feed数组中删除Feed网址。在这种情况下,应删除asdasd。
终端:
irb(main):133:0> feeds = ['http://da.wordpress.org/feed/', 'asdasd']
=> ["http://da.wordpress.org/feed/", "asdasd"]
irb(main):134:0> feeds.each do |u|
irb(main):135:1* begin
irb(main):136:2* open(u)
irb(main):137:2> rescue
irb(main):138:2> feeds.delete(u)
irb(main):139:2> end
irb(main):140:1> end
=> ["asdasd"]
它只删除数组中的第一个元素,而不是删除asdasd ...
答案 0 :(得分:1)
我编写了如下代码,它工作正常:
require 'open-uri'
feeds = ['http://da.wordpress.org/feed/', 'asdasd']
feeds.dup.each do|u|
begin
open(u)
rescue Errno::ENOENT
feeds.delete(u)
end
end
p feeds
# >> ["http://da.wordpress.org/feed/"]
虽然你的版本正常,但这不是一个好习惯。
require 'open-uri'
feeds = ['http://da.wordpress.org/feed/', 'asdasd']
feeds.each do|u|
begin
open(u)
rescue
feeds.delete(u)
end
end
p feeds
# >> ["http://da.wordpress.org/feed/"]