るりまサーチ

最速Rubyリファレンスマニュアル検索!
44件ヒット [1-44件を表示] (0.015秒)
トップページ > モジュール:Enumerable[x] > クエリ:chunk[x] > クエリ:slice_before[x]

別のキーワード

  1. _builtin slice
  2. _builtin slice!
  3. string slice
  4. string slice!
  5. _builtin slice_before

ライブラリ

検索結果

Enumerable#slice_before {|elt| bool } -> Enumerator (18151.0)

パターンがマッチした要素、もしくはブロックが真を返した要素から 次にマッチする手前までを チャンク化(グループ化)したものを繰り返す Enumerator を 返します。

...ャンクは配列として表現されます。

Enumerable
#to_a や Enumerable#map のようなメソッドを使うこ
ともできます。

//emlist[例][ruby]{
# 偶数要素をチャンクの先頭と見なす
[0,2,4,1,2,4,5,3,1,4,2].slice_before(&:even?).to_a
# => [[0], [2], [4, 1], [2], [4,...
...2].slice_before(&:odd?).to_a
# => [[0, 2, 4], [1, 2, 4], [5], [3], [1, 4, 2]]

# ChangeLog のエントリーを順に取る
open("ChangeLog") {|f|
f.slice_before(/\A\S/).each {|e| pp e}
}

# 上と同じだが、パターンでなくブロックを使う
open("ChangeLog") {|f|
f.slice_before {|...
...Enumerable#chunk_while のより簡単な例も参照)。

//emlist[][ruby]{
a = [0,2,3,4,6,7,9]
prev = a[0]
p a.slice_before {|e|
prev, prev2 = e, prev
prev2 + 1 != e
}.map {|es|
es.length <= 2 ? es.join(",") : "#{es.first}-#{es.last}"
}.join(",")
#=> "0,2-4,6,7,9"
//}


@see Enumerable#chunk...

Enumerable#slice_before(pattern) -> Enumerator (18151.0)

パターンがマッチした要素、もしくはブロックが真を返した要素から 次にマッチする手前までを チャンク化(グループ化)したものを繰り返す Enumerator を 返します。

...ャンクは配列として表現されます。

Enumerable
#to_a や Enumerable#map のようなメソッドを使うこ
ともできます。

//emlist[例][ruby]{
# 偶数要素をチャンクの先頭と見なす
[0,2,4,1,2,4,5,3,1,4,2].slice_before(&:even?).to_a
# => [[0], [2], [4, 1], [2], [4,...
...2].slice_before(&:odd?).to_a
# => [[0, 2, 4], [1, 2, 4], [5], [3], [1, 4, 2]]

# ChangeLog のエントリーを順に取る
open("ChangeLog") {|f|
f.slice_before(/\A\S/).each {|e| pp e}
}

# 上と同じだが、パターンでなくブロックを使う
open("ChangeLog") {|f|
f.slice_before {|...
...Enumerable#chunk_while のより簡単な例も参照)。

//emlist[][ruby]{
a = [0,2,3,4,6,7,9]
prev = a[0]
p a.slice_before {|e|
prev, prev2 = e, prev
prev2 + 1 != e
}.map {|es|
es.length <= 2 ? es.join(",") : "#{es.first}-#{es.last}"
}.join(",")
#=> "0,2-4,6,7,9"
//}


@see Enumerable#chunk...

Enumerable#slice_after {|elt| bool } -> Enumerator (13.0)

パターンがマッチした要素、もしくはブロックが真を返した要素を末尾の要素 としてチャンク化(グループ化)したものを繰り返す Enumerator を 返し ます。

..._a
#=> [["foo\n"], ["bar\\\n", "baz\n"], ["\n"], ["qux\n"]]
p e.map {|ll| ll[0...-1].map {|l| l.sub(/\\\n\z/, "") }.join + ll.last }
#=>["foo\n", "barbaz\n", "\n", "qux\n"]
//}

Enumerable
#map のようなメソッドを使うこともできます。

@see Enumerable#chunk, Enumerable#slice_before...

Enumerable#slice_after(pattern) -> Enumerator (13.0)

パターンがマッチした要素、もしくはブロックが真を返した要素を末尾の要素 としてチャンク化(グループ化)したものを繰り返す Enumerator を 返し ます。

..._a
#=> [["foo\n"], ["bar\\\n", "baz\n"], ["\n"], ["qux\n"]]
p e.map {|ll| ll[0...-1].map {|l| l.sub(/\\\n\z/, "") }.join + ll.last }
#=>["foo\n", "barbaz\n", "\n", "qux\n"]
//}

Enumerable
#map のようなメソッドを使うこともできます。

@see Enumerable#chunk, Enumerable#slice_before...