Heads up: This description was created by AI and might not be 100% accurate.
write_file_with_open.rb
This Ruby code snippet demonstrates:
- Loading the
json
library. - Creating a hash with a key-value pair.
- Writing the hash to a file named
file.json
in theinput/json
directory as JSON data. The file is automatically closed after writing.
Ruby code snippet
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)>
Executed with Ruby 3.4.5
.