Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@fairchild
Forked from bakkdoor/file.rb
Created October 30, 2008 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fairchild/21182 to your computer and use it in GitHub Desktop.
Save fairchild/21182 to your computer and use it in GitHub Desktop.
f = File.new("myfile.txt", "r") # open a file with read-mode
# ... do some stuff with the file, like read its content, ouput it on the screen or whatever
# finally: close it!
f.close
# or:
File.new("myfile.txt", "w") do |f|
# now here you can do stuff with f (the file) same as above
# only that you don't have to close the file manually, it gets done automatically once the block has ended.
# the code for closing is inside the constructor of file and gets executed
# a) if there is a block given => after the block has finished
# b) not at all, if no block is given (needs to be called manually)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment