Here are a list of 5 quick ways to gain control of the files you utilize in Ruby. Enjoy!
1. & 2. [.open & .new] – Both of these methods will open a file and return a new File object.
Open has an optional code block that can be applied. If used, the block will be passed the opened file as an argument and the File object will automatically close when the code block terminates.
3. [.absolute_path] – This method does just what it sounds like, returns the absolute path of a file passed in as the method’s argument.
Your path may vary if you are not using a mac or have the same path setup as myself.
4. [.atime] – If you’re interested in finding out the last time a file was accessed this method will return a time object for you.
In the docs, the date format looks more readable (in my opinion) than what the format I have commented out in the above example. I plan on figuring out how to mimic or alter the time format and update this post at a future date.
5. [.delete] – Finally we have the delete method.
And there you have it. A quick-start into manipulating files in Ruby!