From: Akinori MUSHA Date: 2008-11-27T15:22:05+09:00 Subject: [ruby-dev:37210] RSS::Maker.create(version) --pgp-sign-Multipart_Thu_Nov_27_15:26:57_2008-1 Content-Type: text/plain; charset=ISO-2022-JP  RSS::Maker で、 "2.0" 等の文字列でフィードのフォーマットを渡す インターフェースは事実上 RSS::Maker.make(version) {|maker| ... } しかありません。(maker = RSS::Maker::XXXXX.new 相当ができない)  しかし、RSSの生成が処理の主眼でない場合や、複数のRSSフィードを 同時に作ったりする場合、ブロックの導入が必須のインターフェースは 使いづらいです。  そこで、 RSS::Maker.create(version) のようなメソッドを追加する のはどうでしょうか。つまり、 FEED_FORMAT = "2.0" ARTICLES_RSS = File.expand_path("~/public_html/feeds/articles.rss") IMPORTANT_ARTICLES_RSS = File.expand_path("~/public_html/feeds/important_articles.rss") rss = RSS::Maker.make(FEED_FORMAT) { |rssmaker| # ... channel, image等の設定 rss_important = RSS::Maker.make(FEED_FORMAT) { |rssmaker_important| # ... channel, image等の設定 articles.each { |article| rssmaker.new_item { |item| item.title = article.subject # ... link, date等の設定 } if article.important? rssmaker_important.new_item { |item| item.title = article.subject # ... link, date等の設定 } end } } File.open(IMPORTANT_ARTICLES_RSS, "w") { |w| w.print(rss_important.to_s) } } File.open(ARTICLES_RSS, "w") { |w| w.print(rss.to_s) } の本処理部分を rssmaker = RSS::Maker.create(FEED_FORMAT) # ... channel, image等の設定 rssmaker_important = RSS::Maker.make(FEED_FORMAT) # ... channel, image等の設定 articles.each { |article| rssmaker.new_item { |item| item.title = article.subject # ... link, date等の設定 } if article.important? rssmaker_important.new_item { |item| item.title = article.subject # ... link, date等の設定 } end } File.open(IMPORTANT_ARTICLES_RSS, "w") { |w| w.print(rssmaker_important.to_feed.to_s) } File.open(ARTICLES_RSS, "w") { |w| w.print(rssmaker.to_feed.to_s) } のようにフラットな構造で書きたい、というわけです。  以下がパッチです。 Index: lib/rss/maker.rb =================================================================== --- lib/rss/maker.rb (revision 20367) +++ lib/rss/maker.rb (working copy) @@ -5,10 +5,14 @@ module RSS MAKERS = {} class << self - def make(version, &block) + def create(version) m = maker(version) raise UnsupportedMakerVersionError.new(version) if m.nil? - m[:maker].make(m[:version], &block) + m[:maker].new(m[:version]) + end + + def make(version, &block) + create(version).make(&block) end def maker(version) -- Akinori MUSHA / http://akinori.org/ --pgp-sign-Multipart_Thu_Nov_27_15:26:57_2008-1 Content-Type: application/pgp-signature Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (FreeBSD) iEYEABECAAYFAkkuPbEACgkQkgvvx5/Z4e6isQCgxNPWkRgKu6puNsQrDHFuVCG8 GfYAoKvpB4JplSlzBxoXGnUNiS/I9nAm =fFAy -----END PGP SIGNATURE----- --pgp-sign-Multipart_Thu_Nov_27_15:26:57_2008-1--