Ruby is a very versatile language that borrows from the concept that everything is an object. While this method makes working with Ruby more much more flexible and enjoyable to learn and work the way ruby handles files makes it even more powerful to work with.

 If you been learning Ruby or are familiar with it’s class system you are most likely familiar with Ruby’s Input Out (IO) class. This class is responsible for putting things on your screen such as strings. One of it’s widely used method include “puts”, which is on output. Another similar method to “puts”  would be “print” with the difference of returning a line return depending which one you use.  Another come method used that is part of Ruby’s IO class would “gets”. “Gets” is an input which waits for the user’s input. Usually the user input is stored in variable which makes it easier to manipulate within you program. Puts, print and gets makes your program more powers because it allows the user to make decisions and based on those decision your program would act accordingly.

Ruby’s file system work similar to that of most operating systems with  few exceptions that would make it much easier to work with once addressed. These exceptions include the way Ruby handles file path separators and file permissions, which ruby handles different depending on the operating system. Unix file system uses forward slashes for its path while windows uses backslashes. Given these differences ruby makes it safe to always use the forward slashes no matter what operating system you are running.
·      Unix: shared/lib/myfile.rb
·      Windows: Shared\lib\myfile.rb
Ruby’s File class simplifies all this by providing us with the join method. The join method determines what your system can handle whether the forward slashes or the backslashes. 
·      File.join(‘shared’,’lib’,’myfile.fb’)

Working with files in ruby is very simple. Just like working with any other platforms there are some difference that we all need to become aware. It is assumed that every file on your computer belongs to you, with the exceptions of certain situations. 
In Unix we change the files permission using the “chmod” command. (http://ss64.com/bash/chmod.html). In windows permissions are managed via Properties/Security tab. (https://msdn.microsoft.com/en-us/library/bb727008.aspx).

Understanding how to locate files and its permissions helps us have better understanding of ruby’s fundamentals. Now that we got that cover its only natural that we also discus how to accesses the files and read them.
Ruby provided us with two methods to reading files they are File.new and File.open.
With File.new we are creating an object that would exists as a file in your system.  We give it a name and pass it on option. Like any other object we want to assign our new object to a variable.
·      file = File.new(“test_file.txt” , w)   
Below is a list of options that can be used for files.
·      "r"  -  Read-only, starts at beginning of file.
·      "r+" - Read-write, starts at beginning of file.
·      "w"  Write-only, truncates existing file to zero length or creates a  new file for writing. 
·      "w+" Read-write, truncates existing file to zero length or creates a new file for reading and writing. 
·      "a"  Write-only, each write call appends data at end of file.      Creates a new file for writing if file does not exist. 
·      "a+" Read-write, each write call appends data at end of file.      Creates a new file for reading and writing if file does not exist.
It is always good practice to close your files access once you are done reading or writing to them in order to prevent accidental deletions or writes. With our previous example we close the file using the close method, file.close .  The other method that we can used is the open method.
·      File.open (“test_file.txt”, “w”)
This method creates a file with the specified name if it doesn’t exists and closes it for you. It is also possible to pass a code block with the open method.
·      File.open(“test_file.txt”, “r”) do |file|
·      #reads the file data
·      end

Since we went over the basics of creating files in Ruby writing to these files becomes easier. When we create a file or instantiate a file with the same name as one that already exists in the same location we are essentially erasing everything with it unless we use the “r” option.
To write into a file in ruby we first instantiate the file object.
·      file =File.new(“This_is_the_name_of_a_file.txt”,”w”)
Once instantiate we could use various methods of writing to the file.
·      puts    file.puts “This text will go into the file created”
·      print   file.print “This text will go into the file created”
·      <<     file << “This text will go into the file created”
·      write   file.write “This text will go into the file created”
Keep in mind that once we finished writing to the file we must close it in order to execute the methods. Until the file is closed everything stays in the system memory. This is done to preserve the amount of writes done to the local drive.

Thus far we learn on how to access files, create files and write to files. Now we are ready to learn how to read from files.  There are different ways on reading from files in Ruby. We can partially ready from a file, only read an exact amount of characters, read an entire line or the entire document.
·       file = File.new(“test_file.txt”, “r”)   assigns a variable to the read process
·      file.gets  reads from the current pointer position to the end of the line including the line return. Also sets the pointer to the end of the line.
·      file.read(num)  reads the exact amount of characters specified within the parenthesis including white spaces. Sets the pointer where it finished reading.
·      file.each_line{|line| puts line}  reads each line of the file. Sets the pointer at the end of the file.

File pointers in Ruby are very important part to writing and reading to and from files. Pointers tells ruby were to start reading and where to start writing. To find the current position  of the file pointer we use the pos method. We can also set the position of pointer, pos = 2 .
·      file = File.new(“test_file.txt”,”r+”) assigns the file to the variable file with the read and write option.
·      file.pos  returns the pointer status
·      file.pos = 2  sets the pointer position
·      file.eof? returns true or false depending if the pointer is at the end of the file
·      file.rewind  Takes us to the beginning of the file and sets lineno to 0
·      file.pos +=4  moves the pointer 4 position depending were the pointer is located
·      file.pos -=4 moves the pointer back 4 position back depending were the pointer is located
·      file.lineno doesn’t return the line number. It keeps track of how any times gets has been called.
·      ** Be careful on setting the file pointer beyond the end of the file. Setting the pointer beyond the end of the file would replace any an accounted space to nil numeric equivalent of \000\

Renaming files
Renaming files in Ruby is very straightforward. We use the rename method.
·      File.rename(“test_file.txt”, “file_to_delete”) We call the rename method on the File class. We pass it the name of the file follow by the name that we want to rename it with.
Deleting files
Just like renaming files deleting them is just as simple
·      File.delete(“file_to_delete”)  We call the delete method to the File class follow by the name of the file.
Coping files
 Copying files in ruby is just as simple as requiring the FileUtils class from ruby. This class is part of ruby but needs to be required first. Essentially this class allows us to use the most common commands that we are used to using via the command line in Unix.
·      require ‘fileutils’
·      FileUtils.copy (“test_file.txt”, “copied file”)
Beyond copying files FileUtils allows us to change file permissions and provided us with many more options which are normally available via the command line.

Working with files in ruby is very useful and straightforward. The same thinking can be apply when working with directories which I’ll cover in future posts. Mean while getting comfortable with Ruby’s File and IO class are essential to proficiently navigating, reading and writing files in Ruby.