I wrote this little script in PHP a bit ago. It takes a .csv file and outputs a .txt file with certain data arranged in a "psuedo-array" looking format. I recently started messing around with ruby so I decided to redo it. It wound up essentially an exact translation though, I'm wondering if there is a simpler / more ruby way to do this, and of course suggestions as to how to improve my coding in general. New to the whole code review thing. Hope it doesn't show too much.
require "csv"
class ArrayGenerator
def initialize( file )
@file = file
open_csv
end
private
def open_csv
File.open("#{@file}" + "_output.txt", 'w') do |file|
CSV.foreach(@file) do |row|
lat = row[6]
lon = row[7]
img = "#{row[0]}".downcase
part = "#{row[2]}"
loc = "#{row[5]}"
country = "#{row[4]}"
focus = "#{row[3]}"
desc = "#{row[8]}"
link = "#{row[9]}"
file.write("[#{lat}, #{lon}, #{img}, #{part}, #{loc}, #{focus}, #{country}, #{desc}, #{link}],
")
end
end
end
end
ARGV.each do |file|
g = ArrayGenerator.new( file )
end
EDIT: After @sepp2k suggestions code looks like this.
require "csv"
def generate_array( file )
File.open("#{file}" + "_output.txt", 'w') do |output|
CSV.foreach(file) do |img, _, part, focus, country, loc, lat, lon, desc, link|
output.puts("[#{lat}, #{lon}, '#{img.downcase}', '#{part}', '#{loc}', '#{focus}', '#{country}', '#{desc}', '#{link}'],")
end
end
end
ARGV.each do |file|
generate_array(file)
end