Load a single-column CSV file into a Ruby array -
i new ruby.
below naive code load single-column csv file ruby array.
question: there better?
in particular, how not hard-code number of items?
require 'csv' countries = array.new(240) = 0 csv.foreach "#{rails_root}/config/countries.csv" |country| countries[i] = country[0] = + 1 end
try this:
require 'csv' countries = csv.read("#{rails_root}/config/countries.csv").flatten
Comments
Post a Comment