Write_file_with_open.rb
This content was produced by an LLM and could include errors.
このスクリプトはJSONファイルへの書き込み方法の1つです。File.open(..., 'w') のブロック内で JSON.dump(hash, f) を呼び、シリアライズ結果をIOへ直接書き出します。ブロックを抜ければファイルは自動的に閉じられます。
require 'json'
#=> true
hash = {:key => 'value'}
#=> {key: "value"}
File.open('input/json/file.json', 'w') {|f| JSON.dump(hash, f)}
#=> #<File:input/json/file.json (closed)>
Ruby 4.0.6