るりまサーチ

最速Rubyリファレンスマニュアル検索!
11件ヒット [1-11件を表示] (0.045秒)

別のキーワード

  1. argf.class lines
  2. argf.class each
  3. argf.class each_line
  4. argf.class to_a
  5. argf.class gets

検索結果

Enumerable#chunk {|elt| ... } -> Enumerator (21.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]{
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...
...s.\n",
# "\n"]
# ...
//}

テキストを空行で区切られた段落に分けたい場合にも nil が使えます。

//emlist[例][ruby]{
File
.foreach("README").chunk {|line|
/\A\s*\z/ !~ line || nil
}.each {|_, lines|
pp lines
}
//}

「:_alone」は要素を素通ししたい場合...