Heads up: This description was created by AI and might not be 100% accurate.
write_file_with_write.rb
This Ruby code snippet demonstrates how to write a hash to a file in JSON format using the JSON
module in Ruby.
The first line, require 'json'
, loads the JSON library into the current script, allowing us to use its functions for working with JSON data.
Next, we define a hash object hash
with one key-value pair. The key is :key
and the value is 'value'
.
Then, we use the File.write()
method to write the contents of the hash
object to a file named input/json/file.json
. We use the JSON.dump()
method to convert the hash object to JSON format before writing it to the file.
Note that the path to the output file is specified as 'input/json/file.json'
, and the method uses the current working directory of the script as the base path for the file name. If you want to specify a different base path or use an absolute path, you can modify the path accordingly.
Ruby code snippet
require 'json'
#=> true
hash = {:key => 'value'}
#=> {key: "value"}
File.write('input/json/file.json', JSON.dump(hash))
#=> 15
Executed with Ruby 3.4.4
.