Module: DataTools::Enumerator
- Included in:
- Enumerator
- Defined in:
- lib/data_tools/enumerator.rb
Instance Method Summary collapse
- #csvme(outputstream, fields, headers = fields) ⇒ Object
- #lazy_map(&block) ⇒ Object
- #lazy_select(&block) ⇒ Object
Instance Method Details
#csvme(outputstream, fields, headers = fields) ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/data_tools/enumerator.rb', line 2 def csvme(outputstream, fields, headers = fields) outputstream.puts headers.to_csv each do |hash| outputstream.puts hash.pluck(fields).to_csv end outputstream.flush # otherwise missing rows might not get pushed out outputstream rescue Errno::EPIPE # output was closed, that's fine end |
#lazy_map(&block) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/data_tools/enumerator.rb', line 21 def lazy_map(&block) Enumerator.new do |yielder| self.each do |value| yielder.yield(block.call(value)) end end end |
#lazy_select(&block) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/data_tools/enumerator.rb', line 13 def lazy_select(&block) Enumerator.new do |yielder| self.each do |val| yielder.yield(val) if block.call(val) end end end |