EKOSM EKOSM - 3 months ago 10
Ruby Question

Save hash in the csv ruby

I have hash:

users = {
'name' => name,
'last_name' => last_name,
'photo' => photo,
'address' => address,
}


Save in csv:

CSV.open("users.csv", "a") {|csv| users.to_a.each {|elem| csv << elem}}


Output:

name, Jake
last_name,Kallen
photo, http://avs.ru/leto_3434.jpg
address, NEW YORK
name, Dan
last_name,Leans
photo, http://avs.ru/leto_3423.jpg
address, NEW YORK


I need to get the file:

Jake,Kallen,http://avs.ru/leto_3434.jpg,NEW YORK


How to change my code to get what I need?
Help me please.

UPD: I forgot to point out that a lot of users

Answer
CSV.open("users.csv", "a") {|csv|
  csv << users.values
}

I think, you need this. If users hash will contains other data - csv will take it to

Comments