I'm trying to create a json file and write to it.
My code looks like this:
def save_as_json(object) f = File.new('file.json') f.puts(object.to_json, 'w') f.close end save_as_json({'name'=>'fred'}) The problem is, I get the following error when I run it:
:15:in `initialize': No such file or directory @ rb_sysopen - file.json (Errno::ENOENT) I'm asking Ruby to create the file but it's complaining that it doesn't exist! What is the correct way to create and write to a file?
File.write( "file.json", object.to_json ). This will truncate an existing file or create a new file if one doesn't exist, write the contents to it and close the file.