将数组写入文件 ruby

用户2950593

我有很多品牌。

我有这个代码:

brands.each_with_index do |brand, index|
  File.open('brands.txt', 'w') { |file| file.print  "#{brand.name} - #{brand.url}" }
end

所以我想将所有品牌写入文件,但结果我只得到最后一个品牌,如下所示:

Biomill - http://site.com/brand/biomill/dogs/

似乎我的文件正在循环中被重写。我该怎么办?

安东尼

你几乎拥有它!

File.open('brands.txt', 'w') do |file| 
  brands.each_with_index do |brand, index|
    file.print "#{brand.name} - #{brand.url}" 
  end
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章