D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Enumerable
/
Filename :
chunk-i.ri
back
Copy
U:RDoc::AnyMethod[iI" chunk:ETI"Enumerable#chunk;TF:publico:RDoc::Markup::Document:@parts[&o:RDoc::Markup::Paragraph; [I"PEach element in the returned enumerator is a 2-element array consisting of:;To:RDoc::Markup::BlankLine o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0; [o; ; [I"#A value returned by the block.;To;;0; [o; ; [I"RAn array ("chunk") containing the element for which that value was returned, ;TI"Land all following elements for which the block returned the same value:;T@o; ; [I" So that:;T@o;; ;;[o;;0; [o; ; [I"DEach block return value that is different from its predecessor ;TI"begins a new chunk.;To;;0; [o; ; [I"AEach block return value that is the same as its predecessor ;TI"continues the same chunk.;T@o; ; [I" Example:;T@o:RDoc::Markup::Verbatim; [I"De = (0..10).chunk {|i| (i / 3).floor } # => #<Enumerator: ...> ;TI" # The enumerator elements. ;TI" e.next # => [0, [0, 1, 2]] ;TI" e.next # => [1, [3, 4, 5]] ;TI" e.next # => [2, [6, 7, 8]] ;TI"e.next # => [3, [9, 10]] ;T:@format0o; ; [I"T\Method +chunk+ is especially useful for an enumerable that is already sorted. ;TI"QThis example counts words for each initial letter in a large array of words:;T@o;; [I")# Get sorted words from a web page. ;TI"[url = 'https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt' ;TI"&words = URI::open(url).readlines ;TI")# Make chunks, one for each letter. ;TI"Fe = words.chunk {|word| word.upcase[0] } # => #<Enumerator: ...> ;TI" # Display 'A' through 'F'. ;TI"Ae.each {|c, words| p [c, words.length]; break if c == 'F' } ;T;0o; ; [I"Output:;T@o;; [I"["A", 17096] ;TI"["B", 11070] ;TI"["C", 19901] ;TI"["D", 10896] ;TI"["E", 8736] ;TI"["F", 6860] ;T;0o; ; [I"IYou can use the special symbol <tt>:_alone</tt> to force an element ;TI"!into its own separate chuck:;T@o;; [I"a = [0, 0, 1, 1] ;TI"0e = a.chunk{|i| i.even? ? :_alone : true } ;TI"Be.to_a # => [[:_alone, [0]], [:_alone, [0]], [true, [1, 1]]] ;T;0o; ; [I"OFor example, you can put each line that contains a URL into its own chunk:;T@o;; [I"pattern = /http/ ;TI"open(filename) { |f| ;TI"O f.chunk { |line| line =~ pattern ? :_alone : true }.each { |key, lines| ;TI" pp lines ;TI" } ;TI"} ;T;0o; ; [I"BYou can use the special symbol <tt>:_separator</tt> or +nil+ ;TI"Cto force an element to be ignored (not included in any chunk):;T@o;; [I"a = [0, 0, -1, 1, 1] ;TI"2e = a.chunk{|i| i < 0 ? :_separator : true } ;TI"2e.to_a # => [[true, [0, 0]], [true, [1, 1]]] ;T;0o; ; [I"0Note that the separator does end the chunk:;T@o;; [I"a = [0, 0, -1, 1, -1, 1] ;TI"2e = a.chunk{|i| i < 0 ? :_separator : true } ;TI"<e.to_a # => [[true, [0, 0]], [true, [1]], [true, [1]]] ;T;0o; ; [I"RFor example, the sequence of hyphens in svn log can be eliminated as follows:;T@o;; [I"sep = "-"*72 + "\n" ;TI"&IO.popen("svn log README") { |f| ;TI" f.chunk { |line| ;TI" line != sep || nil ;TI" }.each { |_, lines| ;TI" pp lines ;TI" } ;TI"} ;TI"U#=> ["r20018 | knu | 2008-10-29 13:20:42 +0900 (Wed, 29 Oct 2008) | 2 lines\n", ;TI"# "\n", ;TI"D# "* README, README.ja: Update the portability section.\n", ;TI"# "\n"] ;TI"U# ["r16725 | knu | 2008-05-31 23:34:23 +0900 (Sat, 31 May 2008) | 2 lines\n", ;TI"# "\n", ;TI"F# "* README, README.ja: Add a note about default C flags.\n", ;TI"# "\n"] ;TI" # ... ;T;0o; ; [I"BParagraphs separated by empty lines can be parsed as follows:;T@o;; [ I"+File.foreach("README").chunk { |line| ;TI" /\A\s*\z/ !~ line || nil ;TI"}.each { |_, lines| ;TI" pp lines ;TI"};T;0: @fileI"enum.c;T:0@omit_headings_from_table_of_contents_below0I"(chunk {|array| ... } -> enumerator ;T0[ I"();T@�FI"Enumerable;TcRDoc::NormalModule00