: 
http://rubylearning.com/
http://rubylearning.com/satishtalim/tutorial.html
http://rubylearning.com/download/downloads.html
http://rubylearning.com/other/certification.html
http://rubylearning.com/blog/
http://rubylearning.com/other/ruby_news.html
http://rubylearning.com/other/testimonials.html
http://rubylearning.com/jobs/ruby_jobs.html
http://rubylearning.com/other/ruby_gurus.html
http://rubylearning.com/satishtalim/services.html
http://rubylearning.com/contact/contact.html
http://rubylearning.com/satishtalim/about.html


Read/Write Text Files

Let's look at how we can read / write to a text file with the help of a simple program p027readwrite.rb


# Open and read from a text file

File.open('Constructs.rb', 'r') do |f1|

 while line = f1.gets

  puts line

 end

end

 

# Create a new file and write to it

File.open('Test.rb', 'w') do |f2|

 # use "" for inserting a newline between quotation marks

 f2.puts "Created by Satish\nThank God!"

end

The File.open method can open the file in different modes like 'r' Read-only, starts at beginning of file (default); 'r+' Read/Write, starts at beginning of file; 'w' Write-only, truncates existing file to zero length or creates a new file for writing. Please check the online documentation for a full list of modes available. File.open opens a new File if there is no associated block. If the optional block is given, it will be passed file as an argument, and the file will automatically be closed when the block terminates.


Assignment:

Write a Ruby program (call it p028swapcontents.rb) to do the following. Take two text files say A and B. The program should swap the contents of A and B ie. after the program is executed, A should contain B's contents and B should contains A's.


Traversing Directory Trees

The Find module supports top-down traversal of a set of file paths, given as arguments to the find method. If an argument is a directory, then its name and name of all its files and sub-directories will be passed in (in the example below, this would be from where you run this program).


require 'find'

Find.find('./') do |f|

  type = case

         when File.file?(f): "F"

         when File.directory?(f): "D"

         else "?"

         end

  puts "#{type}: #{f}"

end


We shall talk about require soon here.


Random Access

It's quite easy to access a file randomly.  Let's say we have a text file (named hellousa.rb) , the contents of which is shown below:

puts 'Hello USA'

We now need to display the contents of the file from the word USA. Here's how - program p028xrandom.rb:


f = File.new("hellousa.rb")

f.seek(12, IO::SEEK_SET)

print f.readline                 


The seek method of class IO, seeks to a given offset an Integer (first parameter of method) in the stream according to the value of second parameter in the method.


IO::SEEK_CUR
Seeks to first integer number parameter plus current position
IO::SEEK_END Seeks to first integer number parameter plus end of stream (you probably want a negative value for first integer number parameter)
IO::SEEK_SET  Seeks to the absolute location given by first integer number parameter

More on the scope operator :: here.


Does Ruby allow Object Serialization?

Java features the ability to serialize objects, letting you store them somewhere and reconstitute them when needed. Ruby calls this kind of serialization marshaling. Saving an object and some or all of its components is done using the method Marshal.dump. Later on you can reconstitute the object using Marshal.load. Ruby uses marshaling to store session data. Refer topic Object Serialization later on.




Learning Ruby Navigation                                                         <Random Numbers  | TOC  |  Writing our own Class>