Summary: 1/24/2011
1
Learning Ruby
Files
· # Example 1 Read File and close
counter = 1
file = File.new("sowpods.txt", "r")
while (line = file.gets)
puts "#{counter}: #{line}"
counter = counter + 1
Place input file in same
directory as Ruby file (lib)
There is no ++ operator
end
file.close
# Example 2 Pass file to block
File.open("readfile.rb", "r") do |infile|
while (line = infile.gets)
puts "#{counter}: #{line}"
If the optional block is given on