Project

General

Profile

Bug #5393 ยป 0001-more-consistent-doc-block-style-for-enum.c.patch

enum.c doc patch - bt (Bernd Homuth), 10/04/2011 03:32 PM

View differences:

enum.c
/*
* call-seq:
* enum.grep(pattern) -> array
* enum.grep(pattern) {| obj | block } -> array
* enum.grep(pattern) -> array
* enum.grep(pattern) { |obj| block } -> array
*
* Returns an array of every element in <i>enum</i> for which
* <code>Pattern === element</code>. If the optional <em>block</em> is
......
* (1..100).grep 38..44 #=> [38, 39, 40, 41, 42, 43, 44]
* c = IO.constants
* c.grep(/SEEK/) #=> [:SEEK_SET, :SEEK_CUR, :SEEK_END]
* res = c.grep(/SEEK/) {|v| IO.const_get(v) }
* res = c.grep(/SEEK/) { |v| IO.const_get(v) }
* res #=> [0, 1, 2]
*
*/
......
/*
* call-seq:
* enum.count -> int
* enum.count(item) -> int
* enum.count {| obj | block } -> int
* enum.count -> int
* enum.count(item) -> int
* enum.count { |obj| block } -> int
*
* Returns the number of items in <i>enum</i>, where #size is called
* if it responds to it, otherwise the items are counted through
......
* given, counts the number of elements yielding a true value.
*
* ary = [1, 2, 4, 2]
* ary.count #=> 4
* ary.count(2) #=> 2
* ary.count{|x|x%2==0} #=> 3
* ary.count #=> 4
* ary.count(2) #=> 2
* ary.count{ |x| x%2==0 } #=> 3
*
*/
......
/*
* call-seq:
* enum.detect(ifnone = nil) {| obj | block } -> obj or nil
* enum.find(ifnone = nil) {| obj | block } -> obj or nil
* enum.detect(ifnone = nil) -> an_enumerator
* enum.find(ifnone = nil) -> an_enumerator
* enum.detect(ifnone = nil) { |obj| block } -> obj or nil
* enum.find(ifnone = nil) { |obj| block } -> obj or nil
* enum.detect(ifnone = nil) -> an_enumerator
* enum.find(ifnone = nil) -> an_enumerator
*
* Passes each entry in <i>enum</i> to <em>block</em>. Returns the
* first for which <em>block</em> is not false. If no
......
*
* If no block is given, an enumerator is returned instead.
*
* (1..10).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> nil
* (1..100).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> 35
* (1..10).detect { |i| i % 5 == 0 and i % 7 == 0 } #=> nil
* (1..100).detect { |i| i % 5 == 0 and i % 7 == 0 } #=> 35
*
*/
......
/*
* call-seq:
* enum.find_index(value) -> int or nil
* enum.find_index {| obj | block } -> int or nil
* enum.find_index -> an_enumerator
* enum.find_index(value) -> int or nil
* enum.find_index { |obj| block } -> int or nil
* enum.find_index -> an_enumerator
*
* Compares each entry in <i>enum</i> with <em>value</em> or passes
* to <em>block</em>. Returns the index for the first for which the
......
*
* If neither block nor argument is given, an enumerator is returned instead.
*
* (1..10).find_index {|i| i % 5 == 0 and i % 7 == 0 } #=> nil
* (1..100).find_index {|i| i % 5 == 0 and i % 7 == 0 } #=> 34
* (1..10).find_index { |i| i % 5 == 0 and i % 7 == 0 } #=> nil
* (1..100).find_index { |i| i % 5 == 0 and i % 7 == 0 } #=> 34
* (1..100).find_index(50) #=> 49
*
*/
......
/*
* call-seq:
* enum.find_all {| obj | block } -> array
* enum.select {| obj | block } -> array
* enum.find_all -> an_enumerator
* enum.select -> an_enumerator
* enum.find_all { |obj| block } -> array
* enum.select { |obj| block } -> array
* enum.find_all -> an_enumerator
* enum.select -> an_enumerator
*
* Returns an array containing all elements of <i>enum</i> for which
* <em>block</em> is not <code>false</code> (see also
......
* If no block is given, an enumerator is returned instead.
*
*
* (1..10).find_all {|i| i % 3 == 0 } #=> [3, 6, 9]
* (1..10).find_all { |i| i % 3 == 0 } #=> [3, 6, 9]
*
*/
......
/*
* call-seq:
* enum.reject {| obj | block } -> array
* enum.reject -> an_enumerator
* enum.reject { |obj| block } -> array
* enum.reject -> an_enumerator
*
* Returns an array for all elements of <i>enum</i> for which
* <em>block</em> is false (see also <code>Enumerable#find_all</code>).
*
* If no block is given, an enumerator is returned instead.
*
* (1..10).reject {|i| i % 3 == 0 } #=> [1, 2, 4, 5, 7, 8, 10]
* (1..10).reject { |i| i % 3 == 0 } #=> [1, 2, 4, 5, 7, 8, 10]
*
*/
......
/*
* call-seq:
* enum.collect {| obj | block } -> array
* enum.map {| obj | block } -> array
* enum.collect -> an_enumerator
* enum.map -> an_enumerator
* enum.collect { |obj| block } -> array
* enum.map { |obj| block } -> array
* enum.collect -> an_enumerator
* enum.map -> an_enumerator
*
* Returns a new array with the results of running <em>block</em> once
* for every element in <i>enum</i>.
*
* If no block is given, an enumerator is returned instead.
*
* (1..4).collect {|i| i*i } #=> [1, 4, 9, 16]
* (1..4).collect { |i| i*i } #=> [1, 4, 9, 16]
* (1..4).collect { "cat" } #=> ["cat", "cat", "cat", "cat"]
*
*/
......
/*
* call-seq:
* enum.flat_map {| obj | block } -> array
* enum.collect_concat {| obj | block } -> array
* enum.flat_map -> an_enumerator
* enum.collect_concat -> an_enumerator
* enum.flat_map { |obj| block } -> array
* enum.collect_concat { |obj| block } -> array
* enum.flat_map -> an_enumerator
* enum.collect_concat -> an_enumerator
*
* Returns a new array with the concatenated results of running
* <em>block</em> once for every element in <i>enum</i>.
*
* If no block is given, an enumerator is returned instead.
*
* [[1,2],[3,4]].flat_map {|i| i } #=> [1, 2, 3, 4]
* [[1,2],[3,4]].flat_map { |i| i } #=> [1, 2, 3, 4]
*
*/
......
/*
* call-seq:
* enum.to_a -> array
* enum.entries -> array
* enum.to_a -> array
* enum.entries -> array
*
* Returns an array containing the items in <i>enum</i>.
*
......
* call-seq:
* enum.inject(initial, sym) -> obj
* enum.inject(sym) -> obj
* enum.inject(initial) {| memo, obj | block } -> obj
* enum.inject {| memo, obj | block } -> obj
* enum.inject(initial) { |memo, obj| block } -> obj
* enum.inject { |memo, obj| block } -> obj
* enum.reduce(initial, sym) -> obj
* enum.reduce(sym) -> obj
* enum.reduce(initial) {| memo, obj | block } -> obj
* enum.reduce {| memo, obj | block } -> obj
* enum.reduce(initial) { |memo, obj| block } -> obj
* enum.reduce { |memo, obj| block } -> obj
*
* Combines all elements of <i>enum</i> by applying a binary
* operation, specified by a block or a symbol that names a
......
* Examples:
*
* # Sum some numbers
* (5..10).reduce(:+) #=> 45
* (5..10).reduce(:+) #=> 45
* # Same using a block and inject
* (5..10).inject {|sum, n| sum + n } #=> 45
* (5..10).inject { |sum, n| sum + n } #=> 45
* # Multiply some numbers
* (5..10).reduce(1, :*) #=> 151200
* (5..10).reduce(1, :*) #=> 151200
* # Same using a block
* (5..10).inject(1) {|product, n| product * n } #=> 151200
* (5..10).inject(1) { |product, n| product * n } #=> 151200
* # find the longest word
* longest = %w{ cat sheep bear }.inject do |memo,word|
* memo.length > word.length ? memo : word
* end
* longest #=> "sheep"
* longest #=> "sheep"
*
*/
static VALUE
......
/*
* call-seq:
* enum.partition {| obj | block } -> [ true_array, false_array ]
* enum.partition -> an_enumerator
* enum.partition { |obj| block } -> [ true_array, false_array ]
* enum.partition -> an_enumerator
*
* Returns two arrays, the first containing the elements of
* <i>enum</i> for which the block evaluates to true, the second
......
*
* If no block is given, an enumerator is returned instead.
*
* (1..6).partition {|v| v.even? } #=> [[2, 4, 6], [1, 3, 5]]
* (1..6).partition { |v| v.even? } #=> [[2, 4, 6], [1, 3, 5]]
*
*/
......
/*
* call-seq:
* enum.group_by {| obj | block } -> a_hash
* enum.group_by -> an_enumerator
* enum.group_by { |obj| block } -> a_hash
* enum.group_by -> an_enumerator
*
* Returns a hash, which keys are evaluated result from the
* block, and values are arrays of elements in <i>enum</i>
......
*
* If no block is given, an enumerator is returned instead.
*
* (1..6).group_by {|i| i%3} #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
* (1..6).group_by { |i| i%3 } #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
*
*/
......
/*
* call-seq:
* enum.sort -> array
* enum.sort {| a, b | block } -> array
* enum.sort -> array
* enum.sort { |a, b| block } -> array
*
* Returns an array containing the items in <i>enum</i> sorted,
* either according to their own <code><=></code> method, or by using
......
* built-in Schwartzian Transform, useful when key computation or
* comparison is expensive.
*
* %w(rhea kea flea).sort #=> ["flea", "kea", "rhea"]
* (1..10).sort {|a,b| b <=> a} #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
* %w(rhea kea flea).sort #=> ["flea", "kea", "rhea"]
* (1..10).sort { |a,b| b <=> a } #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
*/
static VALUE
......
/*
* call-seq:
* enum.sort_by {| obj | block } -> array
* enum.sort_by -> an_enumerator
* enum.sort_by { |obj| block } -> array
* enum.sort_by -> an_enumerator
*
* Sorts <i>enum</i> using a set of keys generated by mapping the
* values in <i>enum</i> through the given block.
*
* If no block is given, an enumerator is returned instead.
*
* %w{ apple pear fig }.sort_by {|word| word.length}
* %w{apple pear fig}.sort_by { |word| word.length}
* #=> ["fig", "pear", "apple"]
*
* The current implementation of <code>sort_by</code> generates an
......
*
* require 'benchmark'
*
* a = (1..100000).map {rand(100000)}
* a = (1..100000).map { rand(100000) }
*
* Benchmark.bm(10) do |b|
* b.report("Sort") { a.sort }
* b.report("Sort by") { a.sort_by {|a| a} }
* b.report("Sort by") { a.sort_by { |a| a } }
* end
*
* <em>produces:</em>
......
* using the basic <code>sort</code> method.
*
* files = Dir["*"]
* sorted = files.sort {|a,b| File.new(a).mtime <=> File.new(b).mtime}
* sorted = files.sort { |a,b| File.new(a).mtime <=> File.new(b).mtime }
* sorted #=> ["mon", "tues", "wed", "thurs"]
*
* This sort is inefficient: it generates two new <code>File</code>
......
*
* This is exactly what <code>sort_by</code> does internally.
*
* sorted = Dir["*"].sort_by {|f| test(?M, f)}
* sorted = Dir["*"].sort_by { |f| test(?M, f) }
* sorted #=> ["mon", "tues", "wed", "thurs"]
*/
......
/*
* call-seq:
* enum.all? [{|obj| block } ] -> true or false
* enum.all? [{ |obj| block }] -> true or false
*
* Passes each element of the collection to the given block. The method
* returns <code>true</code> if the block never returns
* <code>false</code> or <code>nil</code>. If the block is not given,
* Ruby adds an implicit block of <code>{|obj| obj}</code> (that is
* Ruby adds an implicit block of <code>{ |obj| obj }</code> (that is
* <code>all?</code> will return <code>true</code> only if none of the
* collection members are <code>false</code> or <code>nil</code>.)
*
* %w{ant bear cat}.all? {|word| word.length >= 3} #=> true
* %w{ant bear cat}.all? {|word| word.length >= 4} #=> false
* [ nil, true, 99 ].all? #=> false
* %w{ant bear cat}.all? { |word| word.length >= 3 } #=> true
* %w{ant bear cat}.all? { |word| word.length >= 4 } #=> false
* [ nil, true, 99 ].all? #=> false
*
*/
......
/*
* call-seq:
* enum.any? [{|obj| block } ] -> true or false
* enum.any? [{ |obj| block }] -> true or false
*
* Passes each element of the collection to the given block. The method
* returns <code>true</code> if the block ever returns a value other
* than <code>false</code> or <code>nil</code>. If the block is not
* given, Ruby adds an implicit block of <code>{|obj| obj}</code> (that
* given, Ruby adds an implicit block of <code>{ |obj| obj }</code> (that
* is <code>any?</code> will return <code>true</code> if at least one
* of the collection members is not <code>false</code> or
* <code>nil</code>.
*
* %w{ant bear cat}.any? {|word| word.length >= 3} #=> true
* %w{ant bear cat}.any? {|word| word.length >= 4} #=> true
* [ nil, true, 99 ].any? #=> true
* %w{ant bear cat}.any? { |word| word.length >= 3 } #=> true
* %w{ant bear cat}.any? { |word| word.length >= 4 } #=> true
* [ nil, true, 99 ].any? #=> true
*
*/
......
/*
* call-seq:
* enum.one? [{|obj| block }] -> true or false
* enum.one? [{ |obj| block }] -> true or false
*
* Passes each element of the collection to the given block. The method
* returns <code>true</code> if the block returns <code>true</code>
......
* <code>true</code> only if exactly one of the collection members is
* true.
*
* %w{ant bear cat}.one? {|word| word.length == 4} #=> true
* %w{ant bear cat}.one? {|word| word.length > 4} #=> false
* %w{ant bear cat}.one? {|word| word.length < 4} #=> false
* [ nil, true, 99 ].one? #=> false
* [ nil, true, false ].one? #=> true
* %w{ant bear cat}.one? { |word| word.length == 4 } #=> true
* %w{ant bear cat}.one? { |word| word.length > 4 } #=> false
* %w{ant bear cat}.one? { |word| word.length < 4 } #=> false
* [ nil, true, 99 ].one? #=> false
* [ nil, true, false ].one? #=> true
*
*/
......
/*
* call-seq:
* enum.none? [{|obj| block }] -> true or false
* enum.none? [{ |obj| block }] -> true or false
*
* Passes each element of the collection to the given block. The method
* returns <code>true</code> if the block never returns <code>true</code>
* for all elements. If the block is not given, <code>none?</code> will return
* <code>true</code> only if none of the collection members is true.
*
* %w{ant bear cat}.none? {|word| word.length == 5} #=> true
* %w{ant bear cat}.none? {|word| word.length >= 4} #=> false
* [].none? #=> true
* [nil].none? #=> true
* [nil,false].none? #=> true
* %w{ant bear cat}.none? { |word| word.length == 5 } #=> true
* %w{ant bear cat}.none? { |word| word.length >= 4 } #=> false
* [].none? #=> true
* [nil].none? #=> true
* [nil,false].none? #=> true
*/
static VALUE
enum_none(VALUE obj)
......
/*
* call-seq:
* enum.min -> obj
* enum.min {| a,b | block } -> obj
* enum.min -> obj
* enum.min { |a,b| block } -> obj
*
* Returns the object in <i>enum</i> with the minimum value. The
* first form assumes all objects implement <code>Comparable</code>;
......
*
* a = %w(albatross dog horse)
* a.min #=> "albatross"
* a.min {|a,b| a.length <=> b.length } #=> "dog"
* a.min { |a,b| a.length <=> b.length } #=> "dog"
*/
static VALUE
......
/*
* call-seq:
* enum.max -> obj
* enum.max {|a,b| block } -> obj
* enum.max -> obj
* enum.max { |a,b| block } -> obj
*
* Returns the object in _enum_ with the maximum value. The
* first form assumes all objects implement <code>Comparable</code>;
......
*
* a = %w(albatross dog horse)
* a.max #=> "horse"
* a.max {|a,b| a.length <=> b.length } #=> "albatross"
* a.max { |a,b| a.length <=> b.length } #=> "albatross"
*/
static VALUE
......
/*
* call-seq:
* enum.minmax -> [min,max]
* enum.minmax {|a,b| block } -> [min,max]
* enum.minmax -> [min,max]
* enum.minmax { |a,b| block } -> [min,max]
*
* Returns two elements array which contains the minimum and the
* maximum value in the enumerable. The first form assumes all
......
*
* a = %w(albatross dog horse)
* a.minmax #=> ["albatross", "horse"]
* a.minmax {|a,b| a.length <=> b.length } #=> ["dog", "albatross"]
* a.minmax { |a,b| a.length <=> b.length } #=> ["dog", "albatross"]
*/
static VALUE
......
/*
* call-seq:
* enum.min_by {|obj| block } -> obj
* enum.min_by -> an_enumerator
* enum.min_by { |obj| block } -> obj
* enum.min_by -> an_enumerator
*
* Returns the object in <i>enum</i> that gives the minimum
* value from the given block.
......
* If no block is given, an enumerator is returned instead.
*
* a = %w(albatross dog horse)
* a.min_by {|x| x.length } #=> "dog"
* a.min_by { |x| x.length } #=> "dog"
*/
static VALUE
......
/*
* call-seq:
* enum.max_by {|obj| block } -> obj
* enum.max_by -> an_enumerator
* enum.max_by { |obj| block } -> obj
* enum.max_by -> an_enumerator
*
* Returns the object in <i>enum</i> that gives the maximum
* value from the given block.
......
* If no block is given, an enumerator is returned instead.
*
* a = %w(albatross dog horse)
* a.max_by {|x| x.length } #=> "albatross"
* a.max_by { |x| x.length } #=> "albatross"
*/
static VALUE
......
/*
* call-seq:
* enum.minmax_by {|obj| block } -> [min, max]
* enum.minmax_by -> an_enumerator
* enum.minmax_by { |obj| block } -> [min, max]
* enum.minmax_by -> an_enumerator
*
* Returns two elements array array containing the objects in
* <i>enum</i> that gives the minimum and maximum values respectively
......
* If no block is given, an enumerator is returned instead.
*
* a = %w(albatross dog horse)
* a.minmax_by {|x| x.length } #=> ["dog", "albatross"]
* a.minmax_by { |x| x.length } #=> ["dog", "albatross"]
*/
static VALUE
......
/*
* call-seq:
* enum.each_with_index(*args) {|obj, i| block } -> enum
* enum.each_with_index(*args) -> an_enumerator
* enum.each_with_index(*args) { |obj, i| block } -> enum
* enum.each_with_index(*args) -> an_enumerator
*
* Calls <em>block</em> with two arguments, the item and its index,
* for each item in <i>enum</i>. Given arguments are passed through
......
* If no block is given, an enumerator is returned instead.
*
* hash = Hash.new
* %w(cat dog wombat).each_with_index {|item, index|
* %w(cat dog wombat).each_with_index { |item, index|
* hash[item] = index
* }
* hash #=> {"cat"=>0, "dog"=>1, "wombat"=>2}
......
/*
* call-seq:
* enum.reverse_each(*args) {|item| block } -> enum
* enum.reverse_each(*args) -> an_enumerator
* enum.reverse_each(*args) { |item| block } -> enum
* enum.reverse_each(*args) -> an_enumerator
*
* Builds a temporary array and traverses that array in reverse order.
*
* If no block is given, an enumerator is returned instead.
*
* (1..3).reverse_each {|v| p v }
* (1..3).reverse_each { |v| p v }
*
* produces:
*
......
/*
* call-seq:
* enum.each_entry {|obj| block} -> enum
* enum.each_entry -> an_enumerator
* enum.each_entry { |obj| block } -> enum
* enum.each_entry -> an_enumerator
*
* Calls <i>block</i> once for each element in +self+, passing that
* element as a parameter, converting multiple values from yield to an
......
* yield
* end
* end
* Foo.new.each_entry{|o| p o }
* Foo.new.each_entry{ |o| p o }
*
* produces:
*
......
/*
* call-seq:
* enum.each_slice(n) {...} -> nil
* enum.each_slice(n) -> an_enumerator
* enum.each_slice(n) { ... } -> nil
* enum.each_slice(n) -> an_enumerator
*
* Iterates the given block for each slice of <n> elements. If no
* block is given, returns an enumerator.
*
* e.g.:
* (1..10).each_slice(3) {|a| p a}
* (1..10).each_slice(3) { |a| p a }
* # outputs below
* [1, 2, 3]
* [4, 5, 6]
......
/*
* call-seq:
* enum.each_cons(n) {...} -> nil
* enum.each_cons(n) { ... } -> nil
* enum.each_cons(n) -> an_enumerator
*
* Iterates the given block for each array of consecutive <n>
* elements. If no block is given, returns an enumerator.
*
* e.g.:
* (1..10).each_cons(3) {|a| p a}
* (1..10).each_cons(3) { |a| p a }
* # outputs below
* [1, 2, 3]
* [2, 3, 4]
......
/*
* call-seq:
* enum.each_with_object(obj) {|(*args), memo_obj| ... } -> obj
* enum.each_with_object(obj) -> an_enumerator
* enum.each_with_object(obj) { |(*args), memo_obj| ... } -> obj
* enum.each_with_object(obj) -> an_enumerator
*
* Iterates the given block for each element with an arbitrary
* object given, and returns the initially given object.
......
* If no block is given, returns an enumerator.
*
* e.g.:
* evens = (1..10).each_with_object([]) {|i, a| a << i*2 }
* evens = (1..10).each_with_object([]) { |i, a| a << i*2 }
* #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
*
*/
......
/*
* call-seq:
* enum.zip(arg, ...) -> an_array_of_array
* enum.zip(arg, ...) {|arr| block } -> nil
* enum.zip(arg, ...) -> an_array_of_array
* enum.zip(arg, ...) { |arr| block } -> nil
*
* Takes one element from <i>enum</i> and merges corresponding
* elements from each <i>args</i>. This generates a sequence of
......
/*
* call-seq:
* enum.take_while {|arr| block } -> array
* enum.take_while -> an_enumerator
* enum.take_while { |arr| block } -> array
* enum.take_while -> an_enumerator
*
* Passes elements to the block until the block returns +nil+ or +false+,
* then stops iterating and returns an array of all prior elements.
......
* If no block is given, an enumerator is returned instead.
*
* a = [1, 2, 3, 4, 5, 0]
* a.take_while {|i| i < 3 } #=> [1, 2]
* a.take_while { |i| i < 3 } #=> [1, 2]
*
*/
......
/*
* call-seq:
* enum.drop_while {|arr| block } -> array
* enum.drop_while { |arr| block } -> array
* enum.drop_while -> an_enumerator
*
* Drops elements up to, but not including, the first element for
......
* If no block is given, an enumerator is returned instead.
*
* a = [1, 2, 3, 4, 5, 0]
* a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
* a.drop_while { |i| i < 3 } #=> [3, 4, 5, 0]
*
*/
......
/*
* call-seq:
* enum.cycle(n=nil) {|obj| block } -> nil
* enum.cycle(n=nil) { |obj| block } -> nil
* enum.cycle(n=nil) -> an_enumerator
*
* Calls <i>block</i> for each element of <i>enum</i> repeatedly _n_
......
* If no block is given, an enumerator is returned instead.
*
* a = ["a", "b", "c"]
* a.cycle {|x| puts x } # print, a, b, c, a, b, c,.. forever.
* a.cycle(2) {|x| puts x } # print, a, b, c, a, b, c.
* a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever.
* a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.
*
*/
......
/*
* call-seq:
* enum.chunk {|elt| ... } -> an_enumerator
* enum.chunk(initial_state) {|elt, state| ... } -> an_enumerator
* enum.chunk { |elt| ... } -> an_enumerator
* enum.chunk(initial_state) { |elt, state| ... } -> an_enumerator
*
* Creates an enumerator for each chunked elements.
* The consecutive elements which have same block value are chunked.
......
* The result enumerator yields the block value and an array of chunked elements.
* So "each" method can be called as follows.
*
* enum.chunk {|elt| key }.each {|key, ary| ... }
* enum.chunk(initial_state) {|elt, state| key }.each {|key, ary| ... }
* enum.chunk { |elt| key }.each { |key, ary| ... }
* enum.chunk(initial_state) { |elt, state| key }.each { |key, ary| ... }
*
* For example, consecutive even numbers and odd numbers can be
* splitted as follows.
*
* [3,1,4,1,5,9,2,6,5,3,5].chunk {|n|
* [3,1,4,1,5,9,2,6,5,3,5].chunk { |n|
* n.even?
* }.each {|even, ary|
* }.each { |even, ary|
* p [even, ary]
* }
* #=> [false, [3, 1]]
......
* This method is especially useful for sorted series of elements.
* The following example counts words for each initial letter.
*
* open("/usr/share/dict/words", "r:iso-8859-1") {|f|
* f.chunk {|line| line.ord }.each {|ch, lines| p [ch.chr, lines.length] }
* open("/usr/share/dict/words", "r:iso-8859-1") { |f|
* f.chunk { |line| line.ord }.each { |ch, lines| p [ch.chr, lines.length] }
* }
* #=> ["\n", 1]
* # ["A", 1327]
......
* For example, the sequence of hyphens in svn log can be eliminated as follows.
*
* sep = "-"*72 + "\n"
* IO.popen("svn log README") {|f|
* f.chunk {|line|
* IO.popen("svn log README") { |f|
* f.chunk { |line|
* line != sep || nil
* }.each {|_, lines|
* }.each { |_, lines|
* pp lines
* }
* }
......
*
* paragraphs separated by empty lines can be parsed as follows.
*
* File.foreach("README").chunk {|line|
* File.foreach("README").chunk { |line|
* /\A\s*\z/ !~ line || nil
* }.each {|_, lines|
* }.each { |_, lines|
* pp lines
* }
*
......
* pass other lines, chunk can be used as follows.
*
* pat = /\A[A-Z][A-Za-z0-9_]+\#/
* open(filename) {|f|
* f.chunk {|line| pat =~ line ? $& : :_alone }.each {|key, lines|
* open(filename) { |f|
* f.chunk { |line| pat =~ line ? $& : :_alone }.each { |key, lines|
* if key != :_alone
* print lines.sort.join('')
* else
......
/*
* call-seq:
* enum.slice_before(pattern) -> an_enumerator
* enum.slice_before {|elt| bool } -> an_enumerator
* enum.slice_before(initial_state) {|elt, state| bool } -> an_enumerator
* enum.slice_before(pattern) -> an_enumerator
* enum.slice_before { |elt| bool } -> an_enumerator
* enum.slice_before(initial_state) { |elt, state| bool } -> an_enumerator
*
* Creates an enumerator for each chunked elements.
* The beginnings of chunks are defined by _pattern_ and the block.
......
* method.
* +each+ method can be called as follows.
*
* enum.slice_before(pattern).each {|ary| ... }
* enum.slice_before {|elt| bool }.each {|ary| ... }
* enum.slice_before(initial_state) {|elt, state| bool }.each {|ary| ... }
* enum.slice_before(pattern).each { |ary| ... }
* enum.slice_before { |elt| bool }.each { |ary| ... }
* enum.slice_before(initial_state) { |elt, state| bool }.each { |ary| ... }
*
* Other methods of Enumerator class and Enumerable module,
* such as map, etc., are also usable.
......
* follows.
*
* # iterate over ChangeLog entries.
* open("ChangeLog") {|f|
* f.slice_before(/\A\S/).each {|e| pp e}
* open("ChangeLog") { |f|
* f.slice_before(/\A\S/).each { |e| pp e }
* }
*
* # same as above. block is used instead of pattern argument.
* open("ChangeLog") {|f|
* f.slice_before {|line| /\A\S/ === line }.each {|e| pp e}
* open("ChangeLog") { |f|
* f.slice_before { |line| /\A\S/ === line }.each { |e| pp e }
* }
*
* "svn proplist -R" produces multiline output for each file.
* They can be chunked as follows:
*
* IO.popen([{"LC_ALL"=>"C"}, "svn", "proplist", "-R"]) {|f|
* f.lines.slice_before(/\AProp/).each {|lines| p lines }
* IO.popen([{"LC_ALL"=>"C"}, "svn", "proplist", "-R"]) { |f|
* f.lines.slice_before(/\AProp/).each { |lines| p lines }
* }
* #=> ["Properties on '.':\n", " svn:ignore\n", " svk:merge\n"]
* # ["Properties on 'goruby.c':\n", " svn:eol-style\n"]
......
*
* a = [0,2,3,4,6,7,9]
* prev = a[0]
* p a.slice_before {|e|
* p a.slice_before { |e|
* prev, prev2 = e, prev
* prev2 + 1 != e
* }.map {|es|
* }.map { |es|
* es.length <= 2 ? es.join(",") : "#{es.first}-#{es.last}"
* }.join(",")
* #=> "0,2-4,6,7,9"
......
* # this assumes all characters have same width.
* def wordwrap(words, maxwidth)
* # if cols is a local variable, 2nd "each" may start with non-zero cols.
* words.slice_before(cols: 0) {|w, h|
* words.slice_before(cols: 0) { |w, h|
* h[:cols] += 1 if h[:cols] != 0
* h[:cols] += w.length
* if maxwidth < h[:cols]
......
* text = (1..20).to_a.join(" ")
* enum = wordwrap(text.split(/\s+/), 10)
* puts "-"*10
* enum.each {|ws| puts ws.join(" ") }
* enum.each { |ws| puts ws.join(" ") }
* puts "-"*10
* #=> ----------
* # 1 2 3 4 5
......
* So each mail can be extracted by slice before Unix From line.
*
* # parse mbox
* open("mbox") {|f|
* f.slice_before {|line|
* open("mbox") { |f|
* f.slice_before { |line|
* line.start_with? "From "
* }.each {|mail|
* }.each { |mail|
* unix_from = mail.shift
* i = mail.index("\n")
* header = mail[0...i]
* body = mail[(i+1)..-1]
* body.pop if body.last == "\n"
* fields = header.slice_before {|line| !" \t".include?(line[0]) }.to_a
* fields = header.slice_before { |line| !" \t".include?(line[0]) }.to_a
* p unix_from
* pp fields
* pp body
......
* }
*
* # split mails in mbox (slice before Unix From line after an empty line)
* open("mbox") {|f|
* f.slice_before(emp: true) {|line,h|
* open("mbox") { |f|
* f.slice_before(emp: true) { |line,h|
* prevemp = h[:emp]
* h[:emp] = line == "\n"
* prevemp && line.start_with?("From ")
* }.each {|mail|
* }.each { |mail|
* mail.pop if mail.last == "\n"
* pp mail
* }
    (1-1/1)