write_file_with_open.rb(json)
This Ruby code uses the ‘json’ library to convert a hash (key-value pair) into a JSON format and then writes it to a file named ‘file.json’. The hash contains a single key-value pair, where the key is “:key” and the value is “‘value’”. The File.open block ensures the JSON representation of the hash is dumped into the specified file in write (‘w’) mode.
Execution:
require 'json'
#=> true
hash = {:key => 'value'}
#=> {:key=>"value"}
File.open('file.json', 'w') {|f| JSON.dump(hash, f)}
#=> #<File:file.json (closed)>
Executed with Ruby 3.3.6