My code:
feeds = Website.find(:all).map{|w| w.feed_url}
feeds.each do |u|
begin
open(u)
rescue
feeds.delete(u)
end
end
Example feeds could be: ['http://da.wordpress.org/feed/', 'asdasd']
I want to rescue any error and remove the feed url from the feed array if any error.. In this case asdasd should be removed.
Terminal:
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"]
It just removes the first element in the array instead of removing asdasd...
I wrote the code as below, and it works fine :
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/"]
Although your version is working,but it is not good practice.
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/"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.