我正在尝试遍历 CSV 文件并从行中打印出信息。我有两列标题为“描述”和“状态”。如果我遍历 CSV 文件,我会得到一系列<CSV::Row "description":<description> "status":<status>
对象。我可以使用 访问信息csv_row["description"]
,但不能csv_row.description
。既然 Ruby 是根据每一行的信息创建对象,为什么不能用点表示法访问它?
示例代码:
CSV.foreach(@file.path, headers:true) do |task|
check_box = task["status"] == 0 ? "[ ] " : "[X] "
puts check_box + task["description"]
end