diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-09-24 10:35:35 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-09-24 10:35:35 +0000 |
commit | e6f4473316c528d4db8d131e6c08fc61cb478e69 (patch) | |
tree | 220b21a0da119414a987c9f51db57c443ae17211 /enum.c | |
parent | c4b0b4c91c54b58b02cf94b25d6127b9c3777996 (diff) | |
download | ruby-e6f4473316c528d4db8d131e6c08fc61cb478e69.tar.gz |
rdoc update.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25075 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'enum.c')
-rw-r--r-- | enum.c | 66 |
1 files changed, 48 insertions, 18 deletions
@@ -2160,27 +2160,57 @@ slicebefore_i(VALUE yielder, VALUE enumerator, int argc, VALUE *argv) * } * * If the block needs to maintain state over multiple elements, - * _initial_state_ argument can be used. - * If non-nil value is given, - * it is duplicated for each "each" method invocation of the enumerator. - * The duplicated object is passed to 2nd argument of the block for "slice_before" method.. - * + * local variables can be used. * For example, monotonically increasing elements can be chunked as follows. * - * a = [2, 5, 2, 1, 4, 3, 1, 2, 8, 0] - * enum = a.slice_before(n: 0) {|elt, h| - * prev = h[:n] - * h[:n] = elt + * a = [2, 5, 2, 1, 4, 3, 1, 2, 8, 1] + * n = 0 + * p a.slice_before {|elt, h| + * prev = n + * n = elt * prev > elt - * } - * enum.each {|ary| p ary } - * #=> [2, 5] - * # [2] - * # [1, 4] - * # [3] - * # [1, 2, 8] - * # [0] - * + * }.to_a + * #=> [[2, 5], [2], [1, 4], [3], [1, 2, 8], [1]] + * + * However local variables are not appropriate to maintain state + * if the result enumerator is used twice or more. + * In such case, the last state of the 1st +each+ is used in 2nd +each+. + * _initial_state_ argument can be used to avoid this problem. + * If non-nil value is given as _initial_state_, + * it is duplicated for each "each" method invocation of the enumerator. + * The duplicated object is passed to 2nd argument of the block for + * +slice_before+ method. + * + * # word wrapping + * def wordwrap(words, width) + * # if cols is local variable, 2nd "each" may start with non-zero cols. + * words.slice_before(cols: 0) {|w, h| + * h[:cols] += 1 if h[:cols] != 0 + * h[:cols] += w.length + * if width < h[:cols] + * h[:cols] = w.length + * true + * else + * false + * end + * } + * end + * text = (1..20).to_a.join(" ") + * enum = wordwrap(text.split(/\s+/), 10) + * puts "-"*10 + * enum.each {|ws| puts ws.join(" ") } + * puts "-"*10 + * #=> ---------- + * # 1 2 3 4 5 + * # 6 7 8 9 10 + * # 11 12 13 + * # 14 15 16 + * # 17 18 19 + * # 20 + * # ---------- + * + * mbox contains series of mails which start with Unix From. + * So each mail can be extracted by slice before Unix From. * * # parse mbox * open("mbox") {|f| |