Code Snippet of handling files in Ruby Programming Language

Advertisement
Post Pic

In the following article we can have a look at the ways of creating new files in Ruby programming language. Also you can learn how to open and read already existing files. Finally you will learn how to rename, delete and also get information on an existing file.

Creating a New File in Ruby Language
You can create new files in Ruby using the “new” method that belongs to “file”class. The “new” method can handle two arguments, the first argument is the file name of the new file that you want to create. The second argument indicates how you want to open the file, like whether you want to access it only for reading or just writing or both and so on.

Following are some of the file modes used in Ruby:
r - Read only access.
r+ – Read and write access.
w – Write only access.
w+ – Read and write access
a – Write only access
a+ – Read and write access
b – Binary file mode

Example: to create a new file in “write” mode:

1
File.new("temp.txt", "w")

To Open Existing Files in Ruby
Existing files can be opened using “open” method belonging to the class “File”.

1
file = File.open("temp.txt")

You can open files already created in any of the modes mentioned above.
Example: to open a file in read-only mode:

1
file = File.open("temp.txt", "r")

You can also check whether a file is already open or not using the method “closed?”.

1
file.closed?

Output: Returns True or False, if the file is already open then it returns “True” else “False”.
To close a file, you can use “close” method belonging to class “file”.

1
2
file = File.open("temp.txt", "r")
file.close

How to rename and delete files in Ruby programming language?
To rename or delete files you can use “rename” and “delete” methods respectively.
Example: Creating new file

1
File.new("tempfile.txt", "w")

Example: Renaming an existing file

1
File.rename("tempfile.txt", "newfile.txt")

Example: Deleting an existing file:

1
File.delete("newfile.txt")

How to get information on Files?
To do more than just creating and opening files you need to use other methods in class “file”.
You can find whether a file exits using “exists?” method:

1
File.exists?("temp.txt")

You can check whether a file is really a file or is it some directory, using “file?” method:

1
File.file?("ruby")

To check whether it is a directory, use the “directory?” method:

1
File.directory?("ruby")

To check whether a file is readable, use “readable?” method:

1
File.readable?("temp.txt")

To check whether a file is writable, use “writable ?” method:

1
File.writable?("temp.txt")

To check whether a file is executable, use “executable?” method:

1
File.executable?("temp.txt")

You can get the file size using the “size” method:

1
File.size("temp.txt")

To check whether file is empty, use “zero?” method:

1
File.zero?("temp.txt")

Find out the file type using “ftype” method:

1
File.ftype("readme.txt")
1
File.ftype("../ruby")
1
File.ftype("/dev/sda5")

The above are only basics of handling files in Ruby programming language. There is more in programing than just handling files. You need a lot of interest and hard work to master these. So, all the best to your journey into Ruby programming!

Articles which you would like to read:
1. Remove duplicates from an array using JavaScript
2. Check out the number of Occurrences using JavaScript
3. Adding a div, button to the body tag dynamically using JavaScript DOM
4. Open e-mail message window onclick of a button using JavaScript
5. Free JavaScript database for your browser, Taffy DB
6. JavaScript Visitors Browser Detection Code Snippet
7. Netscape is the ‘navigator.appName’ for Google Chrome
8. Error: Invalid source HTML for this operation
9. System Error: -1072896658 in IE
10. Simple steps to develop AJAX Website – DeveloperSnippets

Related Entries...

You Might Like:

Advertisement

Please let us know your feedback about the article through the comments section below, we want to hear from you about our website. And your comment / advice may help us to serve you better in upcoming articles.

Thank You!
Vivekanand [Founder of DeveloperSnippets]

Leave Your Response

* Name, Email, Comment are Required

Write For Us

Online Sponsors

Start Foreign Exchange Trading today with the Forex Affiliate Program and also you can have access Online Forex News to know what happens every single moment.


Forex Floor


Switch to our mobile site