1

I can't get headers. No error happens. How can I get it properly?

Data:

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

Ruby:

require "csv"

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

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

puts table[headers] # Nothing happens.

1 Answer 1

2

Try table.headers:

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

How are you running this code? table[headers] should return an error:

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>'
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Blender! I could get headers. ; I didn't use irb. I did "ruby **.rb" on the terminal. In the case nothing happened.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.