Arrays with csv file

headers = [id,name,dept]
val_arr = [[1,2,3],[“sunil”,“anil”,“kumar”],[“IT”,“IT”,“IT”]]
i need to write these thing in csv file like the below ways(form)

id name dept
1 sunil IT
2 anil IT
3 kumar IT

Will this produce the correct file?

require ‘csv’

headers = [‘id’,‘name’,‘dept’]
val_arr =
]“data.csv”, “wb”) do |csv|
csv << headers
val_arr.transpose.each{|e| csv << e}

It looks like it will. Not trying to be flip, but the simplest way to find out is to try it and see.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs