RubyTapas #070 - #075
070 break
?
yield, break, ensure
使いどころが分からない
071 break with a Value
★
break name if name =~ /^S/
breakはreturnの値を上書きできる
break "<Not Found (Stopped at #{f.lineno}: '#{line.chomp}')>"
lineno [ラインノ]
072 Tail Part 1: Random Access
★
unixのtailと同等のものを作る
file = open('small.txt')
file.tell
file.seek(11)
file.seek(-4, IO::SEEK_CUR)
file.seek(0, IO::SEEK_END)
file.eof?
file.seek(-512, IO::SEEK_END)
chunk = file.read
chunk.chars.count("\n")
tell 現在位置、seek 位置の移動
073 Tail Part 2: Do-While
★★
Rubyにはdo while無い
同等の書き方
begin
puts "not done yet."
end while !done?
while condition -> begin end -> end white condition
074 Tail Part 3: #rindex
★
index = s.rindex("\n", index-1) # => 13
s[(index+1)..-1] # => "Line 3\n"
rindex [リンデックス]
075 Tail Part 4: copy_stream
?
改行無しの大きいファイルがある場合とか
メモリとか少しずつ読み込む方法とか
IO.copy_stream(file, $stdout)