alkanschtein
alkanschtein

Reputation: 305

Write key value to file from hash

I would like to write the key value inside file as vals. I hope you can help me about it. Thank you.

File.open(resfile,'a+') do |f2|
  gname.each {|key,val| val.each {|x|   f2.write(x) f2.write("\t")} 
    f2.write("\n")}
end 

Upvotes: 0

Views: 174

Answers (2)

Nicola Mingotti
Nicola Mingotti

Reputation: 998

I guess what you mean in this:

h = { "v1" => 1, "v2" => 2}
File.open("./out.dat","w") do |f| 
  h.each { |k,v| f.write("#{k}\t#{v}\n") }
end 

Upvotes: 0

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

result = gname.map { |_key, val| val.join("\t") }.join("\n")
File.write(resfile, result)

Upvotes: 1

Related Questions