4

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?

2
  • Try 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. Commented Dec 8, 2018 at 13:32
  • Sometimes it's a pathing issue. Compare your file path with what you think the file path is with something like: File.expand_path('my_file.rb') Commented Dec 31, 2021 at 12:47

2 Answers 2

8

You just need to open the file using the 'w' mode like this:

f = File.new('file.json', 'w') 

You want to determine the mode based on what you plan to do with the file, but here are your options:

"r" Read-only, starts at beginning of file (default mode).

"r+" Read-write, starts at beginning of file.

"w" Write-only, truncates existing file to zero length or creates a new file for writing.

"w+" Read-write, truncates existing file to zero length or creates a new file for reading and writing.

"a" Write-only, each write call appends data at end of file. Creates a new file for writing if file does not exist.

"a+" Read-write, each write call appends data at end of file. Creates a new file for reading and writing if file does not exist.

IO Docs

Sign up to request clarification or add additional context in comments.

Comments

2

File creation defaults to read mode, so trying to use a filespec that does not exist will result in an error:

2.3.0 :001 > f = File.new 'foo' Errno::ENOENT: No such file or directory @ rb_sysopen - foo 

You need to specify 'w':

2.3.0 :002 > f = File.new 'foo', 'w' => #<File:foo> 

That said, there are easier ways to write to files than to get a file handle using File.new or File.open. The simplest way in Ruby is to call File.write:

File.write('file.json', object.to_json) 

You can use the longer File.open approach if you want; if you do, the simplest approach is to pass a block to File.open:

File.open('file.json', 'w') { |f| f << object.to_json } 

This eliminates the need for you to explicitly close the file; File.open, when passed a block, closes the file for you after the block has finished executing.

3 Comments

Without a block File.open is the same as File.new. ruby-doc.org/core-2.3.0/File.html#method-c-open
Wow, you're right, I should have known that. I will change my answer to reflect that. Sorry...
Note that with a call as trivial as File.write, there is no longer a need to write a separate wrapper method to do it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.