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...
Here is a simple and easily understandable snippet code, to remove duplicates from an existing array ...
Here is a simple snippet on adding a "div" to the body tag dynamically using JavaScript, after going ...
In some scenarios we will have catch hold of number of occurrences while coding, like for example: In ...
As we all know that firebug is a great add-on for Firefox, this is available in all the respective ve ...
Taffy DB, which is well known JavaScript database for your browser, and which is a free and opensourc ...
Most of the developers might have faced this situation where they need to add !important while using ...
This article will help beginners, that is who are new to jQuery and want to learn some tricks about j ...
Below script will lets you to open Ext.Panel (that is Panel) onClick of an anchor link. Its really a ...
Just think what Google Chrome would return when we run browser detection code written in JavaScript ? ...
As you guys already know that Adobe Flash CS3 is awesome enough, in CS3 we can see more features and ...























Leave Your Response