You can write to a JSON file in Ruby using the built-in json
library and the File
class. Here's an example:
main.rb303 chars17 lines
In this example, we first require the json
library. We then define the data that we want to store in the JSON file as a hash.
Next, we use the JSON.generate
method to convert the hash to a JSON-formatted string.
Finally, we create a new file called data.json
using the File.open
method with the mode set to 'w'
, which means that we're opening the file for writing. We then write the JSON string to the file using the File.write
method.
After running this code, a new file called data.json
will be created in the current directory with the following contents:
43 chars2 lines
gistlibby LogSnag