title: Ruby: Why can't I create a new file?
body: <p>I'm trying to create a json file and write to it.</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>def save_as_json(object)
      f = File.new('file.json')
      f.puts(object.to_json, 'w')
      f.close
  end
  
  save_as_json({'name'=&gt;'fred'})
  </code></pre>
  
  <p>The problem is, I get the following error when I run it:</p>
  
  <pre><code>:15:in `initialize': No such file or directory @ rb_sysopen - file.json (Errno::ENOENT)
  </code></pre>
  
  <p>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?</p>