Difference between revisions of "Working with Files in Ruby"

From Techotopia
Jump to: navigation, search
(New page: In the previous chapter we looked at how to work with directories. This chapter we will look in detail at how to create, open and read and write to files in Ruby. We will then learn how to...)
 
(Creating a New File with Ruby)
Line 4: Line 4:
  
 
New files are created in Ruby using the ''new'' method of the ''File'' class. The ''new'' method accepts two arguments, the first being the name of the file to be created and the second being the mode in which  the file is to opened. Supported file modes are shown the following table:
 
New files are created in Ruby using the ''new'' method of the ''File'' class. The ''new'' method accepts two arguments, the first being the name of the file to be created and the second being the mode in which  the file is to opened. Supported file modes are shown the following table:
 +
 +
<table border="1" cellspacing="0" width="100%">
 +
<tr style="background:#efefef;">
 +
<th>Mode<th>Description</th>
 +
<tr>
 +
<td>r<td>Read only access. Pointer is positioned at start of file.</td>
 +
<tr>
 +
<td>r+<td>Read and write access. Pointer is positioned at start of file.</td>
 +
<tr>
 +
<td>w<td>Write only access. Pointer is positioned at start of file. File is created if it does not already exist.</td>
 +
<tr>
 +
<td>w+<td>Read and write access. Pointer is positioned at start of file. File is created if it does not already exist.</td>
 +
<tr>
 +
<td>a<td>Write only access. Pointer is positioned at end of file. File is created if it does not already exist.</td>
 +
<tr>
 +
<td>a+<td>Read and write access. Pointer is positioned at end of file. File is created if it does not already exist.</td>
 +
<tr>
 +
<td>x<td>Create and open for write only. Pointer is positioned at start of file. Return ''false'' if file already exists.</td>
 +
<tr>
 +
<td>x+<td>Create and open for read and write. Pointer is positioned at start of file. Return ''false'' if file already exists.</td>
 +
</table>

Revision as of 15:45, 29 November 2007

In the previous chapter we looked at how to work with directories. This chapter we will look in detail at how to create, open and read and write to files in Ruby. We will then learn how to delete and rename files.

Creating a New File with Ruby

New files are created in Ruby using the new method of the File class. The new method accepts two arguments, the first being the name of the file to be created and the second being the mode in which the file is to opened. Supported file modes are shown the following table:

ModeDescription
rRead only access. Pointer is positioned at start of file.
r+Read and write access. Pointer is positioned at start of file.
wWrite only access. Pointer is positioned at start of file. File is created if it does not already exist.
w+Read and write access. Pointer is positioned at start of file. File is created if it does not already exist.
aWrite only access. Pointer is positioned at end of file. File is created if it does not already exist.
a+Read and write access. Pointer is positioned at end of file. File is created if it does not already exist.
xCreate and open for write only. Pointer is positioned at start of file. Return false if file already exists.
x+Create and open for read and write. Pointer is positioned at start of file. Return false if file already exists.