Don't use CSV for this. CSV is OK when transferring data but it's lacking when being used for configuration data.
Instead use YAML. It's easily read, and great for configuration files. Ruby comes with a YAML parser as part of the Standard Library.
Here's how to get started:
require 'yaml'
data = {
'string' => 'foo',
'array_of_ints' => [1,2,3],
'int' => 0,
'boolean' => true
}
puts data.to_yaml
# >> ---
# >> string: foo
# >> array_of_ints:
# >> - 1
# >> - 2
# >> - 3
# >> int: 0
# >> boolean: true
You can see it's easy to read, and, because it's text, it's easy to modify the YAML file using an editor. Reloading the data is just as easy. If I modify the above code to write to a file I can reload the data and receive a Ruby object containing the data:
require 'yaml'
data = {
'string' => 'foo',
'array_of_ints' => [1,2,3],
'int' => 0,
'boolean' => true
}
File.write('data.yaml', data.to_yaml)
loaded_data = YAML.load_file('data.yaml')
loaded_data # => {"string"=>"foo", "array_of_ints"=>[1, 2, 3], "int"=>0, "boolean"=>true}
loaded_data['string'] # => "foo"
loaded_data['array_of_ints'] # => [1, 2, 3]
loaded_data['int'] # => 0
loaded_data['boolean'] # => true
You can even imitate a CSV file, though I'm not sure why you'd want to since you have to access records sequentially or by index instead of by being able to directly access them from a hash:
loaded_data = YAML.load(<<EOT)
---
- [Name, HR, Avg. ]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa, 63, 0.288]
EOT
loaded_data # => [["Name", "HR", "Avg."], ["Mark McGwire", 65, 0.278], ["Sammy Sosa", 63, 0.288]]