1:#!/usr/bin/ruby -w
   2:
   3:# Created by Michael Williams 12/19/2005
   4:# Licensed under Create Commons Attribution License
   5:
   6:# Example 1 - Read File and close
   7:counter = 1
   8:file = File.new("readfile.rb", "r")
   9:while (line = file.gets)
  10:    puts "#{counter}: #{line}"
  11:    counter = counter + 1
  12:end
  13:file.close
  14:
  15:# Example 2 - Pass file to block
  16:File.open("readfile.rb", "r") do |infile|
  17:    while (line = infile.gets)
  18:        puts "#{counter}: #{line}"
  19:        counter = counter + 1
  20:    end
  21:end
  22:
  23:# Example 3 - Read File with Exception Handling
  24:counter = 1
  25:begin
  26:    file = File.new("readfile.rb", "r")
  27:    while (line = file.gets)
  28:        puts "#{counter}: #{line}"
  29:        counter = counter + 1
  30:    end
  31:    file.close
  32:rescue => err
  33:    puts "Exception: #{err}"
  34:    err
  35:end
  36: