I have a CSV file which I'm parsing and inserting bits of it in a database. I'd like to write a parser which takes account of the fact that the column order may change in the future.
I thought that I could do this by grabbing the header line as an array, and for each line, putting the value in a dynamically created local variable (using eval). However this doesn't seem to work, as local variables don't seem to be accessible outside the eval. I've read elsewhere that this may be correct in ruby 1.9, but I'm using 1.8.7
Code such as:
headers = ["a", "b"]
headers.each do |h|
p e_str = h+"=1"
eval(e_str)
end
puts a
simply doesn't work, giving
test.rb:6: undefined local variable or method `a' for main:Object (NameError)
though line 3 prints "a=1" and "b=1" as expected
Does anyone know how I could go about this?