Monday, November 20, 2006

[temp] Pages 122-3: Iterators for reading


Listen to this article.

Value: Medium

Level: Average

Summary:
You can use special iterators to read data from an IO stream, as IO#each_byte, IO#each_line, IO#foreach, IO#read, IO#readlines.

Memo:
Don’t forget that I/O is never certain in an uncertain world

Example:
IO.foreach("data.txt") {|line| puts line }
# read into string
str = IO.read("data.txt")
# read into an array
arr = IO.readlines("data.txt")

File.open("testfile") do |file|
file.each_byte {|ch| puts ch}
end

File.open("testfile") do |file|
file.each_line {|line| puts "#{line.dump}" }
end

Reported errata (): 0

Errata I found: 3

My suggestions to the author: 0

0 Comments:

Post a Comment

<< Home