How do you create an enumerator wrapper class?
I have this function:
def file_parser (filename)
Enumerator.new do |yielder|
File.open(filename, "r:ISO-8859-1") do |file|
csv = CSV.new(file, :col_sep => "\t", :headers => true, :quote_char
=> "\x07")
csv.each do |row|
yielder.yield map_fields(clean_data(row.to_hash))
end
end
end
end
I can use it like this:
parser = file_parser("data.tab")
parser.each do { |data| #do profitable things with data }
Instead, I'd like to put it in its own class and use it like this:
parser = FancyParser.new("data.tab")
parser.each do { |data| #do profitable things with data }
I've tried some things I didn't expect to work, like just returning the
enumerator out of initialize(), and self = file_parser().
I've also tried super do |yielder|.
For some reason, the way to do this is not coming to me.
No comments:
Post a Comment