create a file in ruby

To create a file in Ruby, use the File.new method with the file path and mode as arguments. The mode specifies the purpose of the file, whether to write, read, or append. Here's an example to create a new file in write mode and write to it:

main.rb
file = File.new("example.txt", "w")
file.puts("Hello, world!")
file.close
74 chars
4 lines

This will create a new file named "example.txt" in write mode, write the string "Hello, world!" to it, and then close the file.

To create a file in append mode, use "a" instead of "w" in the mode argument. And to read from a file, use "r".

related categories

gistlibby LogSnag