Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active September 4, 2019 15:00
Show Gist options
  • Select an option

  • Save krisleech/4e9f9e39d9cf91925aca6963295e738e to your computer and use it in GitHub Desktop.

Select an option

Save krisleech/4e9f9e39d9cf91925aca6963295e738e to your computer and use it in GitHub Desktop.
CSV example
header, *rows = CSV.read(Rails.root.join('my-data.csv'))

rows.map! { |row| Hash[header.zip(row)] } # convert to Array of Hash

rows.map do |row|
  row.transform_values(&method(:format_value))
end

def format_value(value)
  return '' if value.nil?
  
  case value.class.to_s
  when "Date"
    value.strftime('%d/%m/%Y')
  when "TrueClass"
    "Yes"
  when "FalseClass"
    "No"
  else
    value
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment