Open and closing a file

suggest change

Manually open and close a file.

# Using new method
f = File.new("test.txt", "r") # reading
f = File.new("test.txt", "w") # writing
f = File.new("test.txt", "a") # appending

# Using open method
f = open("test.txt", "r")

# Remember to close files
f.close

Automatically close a file using a block.

f = File.open("test.txt", "r") do |f|
  # do something with file f
  puts f.read # for example, read it
end

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents