1

我无法获得标题。没有错误发生。我怎样才能正确地得到它?

数据:

"A","B","C"
"1","2","3"

红宝石:

require "csv"

table = CSV.read("filename", :headers => true)

puts table[0] # "1","2","3"

puts table[headers] # Nothing happens.
4

1 回答 1

2

尝试table.headers

irb(main):006:0> table.headers
=> ["A", "B", "C"]

你是如何运行这段代码的?table[headers]应该返回一个错误:

irb(main):008:0> table[headers]
NameError: undefined local variable or method `headers' for main:Object
    from (irb):8
    from /usr/bin/irb:12:in `<main>'
于 2013-03-11T10:26:05.013 回答