I am trying to loop through a CSV file and print out information from the rows. I have two columns with headers "description" and "status". If I loop through the CSV file, I get a series of <CSV::Row "description":<description> "status":<status>
objects. I can access the info by using csv_row["description"]
, but not csv_row.description
. Since Ruby is creating objects out of the information on each line, why isn't it accessible with dot notation?
Sample code:
CSV.foreach(@file.path, headers:true) do |task|
check_box = task["status"] == 0 ? "[ ] " : "[X] "
puts check_box + task["description"]
end
CSV
doesn't work that way but you could useOpenStruct
:Or just create a custom class that fits your needs, e.g. via
Struct
: