カテゴリ別 2003年 | 2004年 | 2005年 | 2006年 | 2007年 | 2008年
知り合いサイト: よんだもの / 暴想 / Linuxでやる夫 / 新宿Vipper / 僕だけが幸せになればいいのに。
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
![]()
This work is licensed under a
Creative Commons License
(note: text only. w/o web design, citations, (re)distributed softwares).