Next | 5.0 Basic Input and Output | Prev |
The Kernel module has a whole set of I/O-related methods: gets, open, print, printf, putc, puts, readline, readlines, and test.
print "Enter your age: "; age = gets
The class IO may be used to handle both input and output. Subclasses: File and BasicSocket. Work with a file using new and close methods:
aFile = File.new("example.txt", "r") # ... process the file i = 0; aFile.each_byte {|ch| print ch.chr if (i += 1) % 2 == 0 } aFile.close
Or supply the block to open method (closes the file automatically):
File.open("example.txt", "r") do |aFile| # ... process the file aFile.each_line {|line| puts "Got #{ line.dump }" } end
Next | Ruby for Perl programmers | Prev |