I am getting an error from my code. Could anyone point me what wrong I
made here -
require 'csv'
require 'date'
src = <<csv
Date, Time, Open, Close, High, Low, Volume
2007.01.02,07:00,119.01,119.01,119.01,119.01,8
2008.01.02,07:01,119.01,119.01,119.01,119.01,8
2007.01.02,08:02,119.01,119.01,119.01,119.01,8
2010.01.02,07:03,119.01,119.02,119.01,119.02,8
2011.01.02,07:04,119.02,119.03,119.01,119.02,8
csv
File.write('test.csv',src)
CSV::Converters[:weekday] = lambda do |s|
begin
Date.parse(s)
rescue ArgumentError
Date.strptime(s, '%Y.%m.%d').wday
end
end
CSV::Converters[:time_am_pm] = lambda do |s|
begin
Date.parse(s)
rescue ArgumentError
Date.strptime(s, '%H:%M').strftime("%A")
end
end
CSV.foreach('test.csv', :converters => [:weekday,
:numeric,:time_am_pm],:headers => true) do |row|
p row
end
# ~> -:19:in `strptime': invalid date (ArgumentError)
# ~> from -:19:in `rescue in block in <main>'
# ~> from -:16:in `block in <main>'
···
--
Posted via http://www.ruby-forum.com/.