问题描述:

I am using csv to export data. I am dynamically populating the fields and in order to do so i have added loop in to csv:

csv_data = CSV.generate do |csv|

csv << [

@userHeaders.each do |user|

"#{user.name}"

end

]

end

but this returns some thing like this: [#<User >, #<User >, #<User >, #<User >]

but when i tried to inspect the value after assigning it to a variable, it displays all the names that have been passed.

name = "#{user.name}"

puts name.inspect # (this diaplays all the names)

is there any format to display the data while looping inside csv?

please guide with answer

thanks

网友答案:

Try just using the map (aka collect) method of your array:

csv_data = CSV.generate do |csv|
  csv << @userHeaders.map { |user| user.name }
end
相关阅读:
Top