Home

About

Advanced Search

Browse by Discipline

Scientific Societies

E-print Alerts

Add E-prints

E-print Network
FAQHELPSITE MAPCONTACT US


  Advanced Search  

 
Learning Ruby # Example 1 Read File and close
 

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

  

Source: Allan, Vicki H. - Department of Computer Science, Utah State University

 

Collections: Computer Technologies and Information Sciences