how-to-make-stock-trading-system.dogwood008.com
# csv_base.rb require 'csv' class CsvBase def initialize(file_path) @file_path = file_path end def each_line open(@file_path, 'r') do |f| CSV.foreach(@file_path, headers: header?, encoding: encoding) do |row| yield(row) end end end def header? raise NotImplementedError end def encoding 'utf-8' end end
# sbi.rb require './csv_base' class Sbi < CsvBase def header? true end def encoding 'sjis:utf-8' end end
pry
での実行結果
[3] pry(main)> require'./sbi'; require'pp'; Sbi.new('7974_2021-09-09.csv').each_line { |row| pp row }
(前略) #<CSV::Row "日付":"20210909" "時間":"0900" "出来高":" 100" "約定値":" 54460"> #<CSV::Row "日付":"20210909" "時間":"0900" "出来高":" 200" "約定値":" 54450"> #<CSV::Row "日付":"20210909" "時間":"0900" "出来高":" 100" "約定値":" 54460"> #<CSV::Row "日付":"20210909" "時間":"0900" "出来高":" 100" "約定値":" 54500"> #<CSV::Row "日付":"20210909" "時間":"0900" "出来高":" 100" "約定値":" 54490"> #<CSV::Row "日付":"20210909" "時間":"0900" "出来高":" 100" "約定値":" 54460"> #<CSV::Row "日付":"20210909" "時間":"0900" "出来高":" 100" "約定値":" 54450"> #<CSV::Row "日付":"20210909" "時間":"0900" "出来高":" 100" "約定値":" 54460"> #<CSV::Row "日付":"20210909" "時間":"0900" "出来高":" 100" "約定値":" 54530"> #<CSV::Row "日付":"20210909" "時間":"0900" "出来高":" 47800" "約定値":" 54470">