Project Ramon

A learning journey from a Ruby noob perspective

Nailing Files with Ruby – In 5 methods or less


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 docsthe 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!


Categories: AirPair, Newbie, Ruby

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s