Wednesday, September 18, 2013

Add array items to array in one line

Add array items to array in one line

I am building a CSV object and I have a dynamic set of header items. I
build this 'header' row as follows:
headers = ["name", "email"]
questions.each do |q|
headers << q
end
csv << headers
Although this works just fine, what I would like to do is have the call in
one line without having to add the headers variable first.
So something like:
csv << ["name", "email", questions.each { |q| q }]
Obviously the above doesn't work because the each returns the 'questions'
array.
Any suggestions?

No comments:

Post a Comment