Quick reference for efficient coding
This Ruby code snippet reads data from a CSV file named ‘file.csv’. Let’s break it down step by step:
require 'csv'
: This line imports the CSV library, which provides functionality for reading and writing CSV files in Ruby.
CSV.read('file.csv', encoding: "BOM|UTF-8", headers: true)
: This line reads the CSV file named ‘file.csv’. The encoding: "BOM|UTF-8"
argument specifies the encoding of the file, ensuring proper handling of UTF-8 characters with a Byte Order Mark (BOM) if present. The headers: true
argument indicates that the first row of the CSV file contains headers.
.map(&:to_h)
: This part of the code uses map
to iterate over each row of the CSV data and convert it to a hash. &:to_h
is shorthand for passing the to_h
method as a block to map
, which converts each row to a hash where the keys are the header names and the values are the corresponding row values.
So, overall, this code reads data from a CSV file, interprets the first row as headers, and returns an array of hashes where each hash represents a row of data with the headers as keys.
require 'csv'
#=> true
CSV.read('file.csv', encoding: "BOM|UTF-8", headers: true).map(&:to_h)
#=>
[{"key"=>"key1", "value"=>"value1"},
{"key"=>"key2", "value"=>"value2"},
{"key"=>"key3", "value"=>"value3"}]
Executed with Ruby 3.3.5