hitode909の日記

以前はプログラミング日記でしたが、今は子育て日記です

eachの進捗を教えてくれるgem

eachの進捗を教えてくれるgemを作った.eachがどれくらい進んでるか,warnしたり,loggerに出したりしてくれる.
例えば,何件くらい処理するのか分からなかったり,eachの中で時間がかかったりするときとかに便利.

require 'each-with-logging'
require 'open-uri'
require 'nokogiri'

Nokogiri(open('http://b.hatena.ne.jp/')).search('#hotentry h3 .entry-link').each_with_logging{|link|
  p [link['href'], open(link['href']).read.length]
}

eachの代わりに,each_with_loggingにすると,今どこで,何をやってて,どのくらい進んでるかを教えてくれる.

example.rb:8:in `each_with_logging' 1 / 10
["http://anond.hatelabo.jp/20110306221615", 91522]
example.rb:8:in `each_with_logging' 2 / 10
["http://matome.naver.jp/odai/2129920304031289601", 109835]
example.rb:8:in `each_with_logging' 3 / 10
["http://9jp.info/archives/8254", 32567]
example.rb:8:in `each_with_logging' 4 / 10
["http://d.hatena.ne.jp/fujipon/20110306#p1", 238390]
example.rb:8:in `each_with_logging' 5 / 10
["http://d.hatena.ne.jp/gamella/20110305/1299330099", 52127]
example.rb:8:in `each_with_logging' 6 / 10
["http://digimaga.net/2011/03/ted-are-we-in-control-of-our-own-decisions", 20596]
example.rb:8:in `each_with_logging' 7 / 10
["http://jp.wsj.com/japanrealtime/2011/03/07/%E5%89%8D%E5%8E%9F%E5%A4%96%E7%9B%B8%E3%82%92%E8%BE%9E%E4%BB%BB%E3%81%AB%E8%BF%BD%E3%81%84%E3%82%84%E3%81%A3%E3%81%9F20%E4%B8%87%E5%86%86%E3%81%AE%E7%8C%AE%E9%87%91/", 75465]
example.rb:8:in `each_with_logging' 8 / 10
["http://alfalfalfa.com/archives/2468252.html", 427932]
example.rb:8:in `each_with_logging' 9 / 10
["http://music2chnews.blog123.fc2.com/blog-entry-387.html", 84989]
example.rb:8:in `each_with_logging' 10 / 10
["http://coliss.com/articles/build-websites/operation/javascript/jquery-plugins-best-2011-mar.html", 43374]

引数にloggerを渡すと,loggerに書いてくれる.

require 'each-with-logging'
require 'open-uri'
require 'nokogiri'
require 'logger'

logger = Logger.new(STDERR)
Nokogiri(open('http://b.hatena.ne.jp/')).search('#hotentry h3 .entry-link').each_with_logging(logger){|link|
  p [link['href'], open(link['href']).read.length]
}
D, [2011-03-07T13:53:20.586208 #6165] DEBUG -- : example2.rb:9:in `each_with_logging' 1 / 10
["http://anond.hatelabo.jp/20110306221615", 91522]
D, [2011-03-07T13:53:21.401753 #6165] DEBUG -- : example2.rb:9:in `each_with_logging' 2 / 10
["http://matome.naver.jp/odai/2129920304031289601", 109591]
D, [2011-03-07T13:53:22.297306 #6165] DEBUG -- : example2.rb:9:in `each_with_logging' 3 / 10
["http://9jp.info/archives/8254", 32567]
D, [2011-03-07T13:53:22.745299 #6165] DEBUG -- : example2.rb:9:in `each_with_logging' 4 / 10
["http://d.hatena.ne.jp/fujipon/20110306#p1", 238934]
D, [2011-03-07T13:53:24.214217 #6165] DEBUG -- : example2.rb:9:in `each_with_logging' 5 / 10
["http://d.hatena.ne.jp/gamella/20110305/1299330099", 52315]
D, [2011-03-07T13:53:24.728969 #6165] DEBUG -- : example2.rb:9:in `each_with_logging' 6 / 10
["http://digimaga.net/2011/03/ted-are-we-in-control-of-our-own-decisions", 20596]
D, [2011-03-07T13:53:26.094965 #6165] DEBUG -- : example2.rb:9:in `each_with_logging' 7 / 10
["http://jp.wsj.com/japanrealtime/2011/03/07/%E5%89%8D%E5%8E%9F%E5%A4%96%E7%9B%B8%E3%82%92%E8%BE%9E%E4%BB%BB%E3%81%AB%E8%BF%BD%E3%81%84%E3%82%84%E3%81%A3%E3%81%9F20%E4%B8%87%E5%86%86%E3%81%AE%E7%8C%AE%E9%87%91/", 75465]
D, [2011-03-07T13:53:27.051563 #6165] DEBUG -- : example2.rb:9:in `each_with_logging' 8 / 10
["http://alfalfalfa.com/archives/2468252.html", 427930]
D, [2011-03-07T13:53:28.843463 #6165] DEBUG -- : example2.rb:9:in `each_with_logging' 9 / 10
["http://music2chnews.blog123.fc2.com/blog-entry-387.html", 84989]
D, [2011-03-07T13:53:31.648421 #6165] DEBUG -- : example2.rb:9:in `each_with_logging' 10 / 10
["http://coliss.com/articles/build-websites/operation/javascript/jquery-plugins-best-2011-mar.html", 43374]


gemで入る.

gem install each-with-logging

https://github.com/hitode909/each-with-logging