フォーチュンサモナーズ
«前の日記(2007-03-17) 最新 次の日記(2007-03-19)» 編集

Don'tStopMusic

2003|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|12|
2006|01|02|03|04|05|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|08|09|10|

カテゴリ別 2003年 | 2004年 | 2005年 | 2006年 | 2007年 | 2008年

知り合いサイト: よんだもの / 暴想 / Linuxでやる夫 / 新宿Vipper / 僕だけが幸せになればいいのに。


2007-03-18

_ [Ruby] SimpleCSV 0.1.3 このエントリーを含むブックマーク

SimpleCSV 0.1.3 をリリースしました。サブクラスでコールバックメソッドを定義することでパースの挙動をカスタマイズできるようになりました。

class PrintFirstLetterParser < SimpleCSV
  def on_field(str, pr)
    print "#{str[0, 1]} "
  end
  def on_row(str, pr)
  end
end
PrintFirstLetterParser.new.parse(<<_CSV_)
first, second, third, fourth, fifth
_CSV_
#=> f s t f f 

on_field と on_row の第二引数は parse メソッドに渡されたブロックです。デフォルトの挙動を再定義すると以下のようなコードになります。

class DefaultParser < SimpleCSV
  def initialize
    @row = []
  end
  def on_field(str, pr)
    @row << str
  end
  def on_row(str, pr)
    pr.call(@row)
    @row = []
  end
end

個人的にはデフォルトの挙動で十分なのでこの部分のテストが不十分という罠。

[]

最近のコメント:

RSS
Creative Commons License
This work is licensed under a Creative Commons License
(note: text only. w/o web design, citations, (re)distributed softwares).