Skip to content

Instantly share code, notes, and snippets.

@quanon
Created July 24, 2018 09:07
Show Gist options
  • Save quanon/43f3f9191c8048f3e7bb568f2d497201 to your computer and use it in GitHub Desktop.
Save quanon/43f3f9191c8048f3e7bb568f2d497201 to your computer and use it in GitHub Desktop.
CSV から値を取得するのに便利なクラス
require 'csv'
require 'forwardable'
class CSVRows
extend Forwardable
attr_reader :csv_path
def_delegators :each, *Enumerable.instance_methods(false)
def initialize(csv_path)
@csv_path = csv_path
end
def each
return enum_for(:each) unless block_given?
File.open(csv_path, 'rb:Shift_JIS:UTF-8', undef: :replace) do |f|
CSV.new(f).each do |row|
yield(row)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment