12件ヒット
[1-12件を表示]
(0.077秒)
ライブラリ
- ビルトイン (12)
検索結果
-
Enumerable
# chunk {|elt| . . . } -> Enumerator (26.0) -
要素を前から順にブロックで評価し、その結果によって 要素をチャンクに分けた(グループ化した)要素を持つ Enumerator を返します。
...め upcase しています。
//emlist[例][ruby]{
# ファイルのエンコーディングは実際のファイルに合わせてください。
open("/usr/share/dict/words", "r:iso-8859-1") {|f|
f.chunk {|line| line[0].upcase }.each {|ch, lines| p [ch, lines.length] }
}
# => ["A", 17096]
#......で始まるシンボルを指定した場合は例外が発生します。
//emlist[例][ruby]{
[1, 2].chunk { |item| :_underscore }.to_a
# => RuntimeError: symbols beginning with an underscore are reserved
# 「.to_a」無しだと Enumerator を返すのみで例外は発生しない
//}
nil......の出力のハイフンの所で区切りたい場合を考えます。
//emlist[例][ruby]{
sep = "-"*72 + "\n" # ハイフンが72個の行
IO.popen("svn log README") {|f|
f.chunk {|line|
line != sep || nil
}.each {|_, lines|
pp lines
}
}
#=> ["r20018 | knu | 2008-10-29 13:20:42 +0...