我有一个用 Nokogiri 创建的哈希,我正在尝试从中生成一个 JSON 文件。我发现
tempHash = {
"key_a" => "val_a",
"key_b" => "val_b"
}
File.open("public/temp.json","w") do |f|
f.write(tempHash.to_json)
end
问题是我不断收到错误
test.rb:43:in `initialize': No such file or directory - public/temp.json (Errno::ENOENT)
我的代码看起来像
def summary
listing_data = @nodes
listings = listing_data.css('div.unsponsored div.item.compact.listing')
listing_hashes = listings.map do |x|
type = "#{@type}"
address = x.css('div.body h3 a').text
unit = x.css('div.body h3 a').text.gsub!(/.*?(?=#)/im, "")
url = x.css('div.item_inner div.body h3 a').text
price = x.css('h3 span').text
{
:type => type,
:address => address,
:unit => unit,
:url => url,
:price => price,
}
end
File.open("public/temp.json","w") do |f|
f.write(JSON.pretty_generate(listing_hashes))
end
end
谢谢!