Linux heracles.o2switch.net 4.18.0-553.62.1.lve.el8.x86_64 #1 SMP Mon Jul 21 17:50:35 UTC 2025 x86_64
/
opt
/
alt
/
ruby21
/
share
/
ri
/
2.1.0
/
system
/
Enumerable
/
//opt/alt/ruby21/share/ri/2.1.0/system/Enumerable/slice_before-i.ri
U:RDoc::AnyMethod[iI"slice_before:ETI"Enumerable#slice_before;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"6Creates an enumerator for each chunked elements. ;TI"EThe beginnings of chunks are defined by _pattern_ and the block.;To:RDoc::Markup::BlankLine o; ; [I"PIf <code>_pattern_ === _elt_</code> returns <code>true</code> or the block ;TI"Nreturns <code>true</code> for the element, the element is beginning of a ;TI"chunk.;T@o; ; [I"SThe <code>===</code> and _block_ is called from the first element to the last ;TI"Eelement of _enum_. The result for the first element is ignored.;T@o; ; [I"DThe result enumerator yields the chunked elements as an array. ;TI"/So +each+ method can be called as follows:;T@o:RDoc::Markup::Verbatim; [I"3enum.slice_before(pattern).each { |ary| ... } ;TI"9enum.slice_before { |elt| bool }.each { |ary| ... } ;TI"Oenum.slice_before(initial_state) { |elt, state| bool }.each { |ary| ... } ;T:@format0o; ; [I"BOther methods of the Enumerator class and Enumerable module, ;TI"(such as map, etc., are also usable.;T@o; ; [I"IFor example, iteration over ChangeLog entries can be implemented as ;TI" follows:;T@o;; [I"'# iterate over ChangeLog entries. ;TI"open("ChangeLog") { |f| ;TI"0 f.slice_before(/\A\S/).each { |e| pp e } ;TI"} ;TI" ;TI"B# same as above. block is used instead of pattern argument. ;TI"open("ChangeLog") { |f| ;TI"C f.slice_before { |line| /\A\S/ === line }.each { |e| pp e } ;TI"} ;T; 0o; ; [I"@"svn proplist -R" produces multiline output for each file. ;TI"$They can be chunked as follows:;T@o;; [ I"@IO.popen([{"LC_ALL"=>"C"}, "svn", "proplist", "-R"]) { |f| ;TI"? f.lines.slice_before(/\AProp/).each { |lines| p lines } ;TI"} ;TI"E#=> ["Properties on '.':\n", " svn:ignore\n", " svk:merge\n"] ;TI"># ["Properties on 'goruby.c':\n", " svn:eol-style\n"] ;TI"T# ["Properties on 'complex.c':\n", " svn:mime-type\n", " svn:eol-style\n"] ;TI"@# ["Properties on 'regparse.c':\n", " svn:eol-style\n"] ;TI" # ... ;T; 0o; ; [ I"BIf the block needs to maintain state over multiple elements, ;TI""local variables can be used. ;TI"OFor example, three or more consecutive increasing numbers can be squashed ;TI"as follows:;T@o;; [I"a = [0, 2, 3, 4, 6, 7, 9] ;TI"prev = a[0] ;TI"p a.slice_before { |e| ;TI" prev, prev2 = e, prev ;TI" prev2 + 1 != e ;TI"}.map { |es| ;TI"@ es.length <= 2 ? es.join(",") : "#{es.first}-#{es.last}" ;TI"}.join(",") ;TI"#=> "0,2-4,6,7,9" ;T; 0o; ; [ I"CHowever local variables are not appropriate to maintain state ;TI"5if the result enumerator is used twice or more. ;TI"QIn such a case, the last state of the 1st +each+ is used in the 2nd +each+. ;TI"EThe _initial_state_ argument can be used to avoid this problem. ;TI"3If non-nil value is given as _initial_state_, ;TI"Kit is duplicated for each +each+ method invocation of the enumerator. ;TI"FThe duplicated object is passed to 2nd argument of the block for ;TI"+slice_before+ method.;T@o;; [ I"D# Word wrapping. This assumes all characters have same width. ;TI"#def wordwrap(words, maxwidth) ;TI"O # if cols is a local variable, 2nd "each" may start with non-zero cols. ;TI", words.slice_before(cols: 0) { |w, h| ;TI"( h[:cols] += 1 if h[:cols] != 0 ;TI" h[:cols] += w.length ;TI" if maxwidth < h[:cols] ;TI" h[:cols] = w.length ;TI" true ;TI" else ;TI" false ;TI" end ;TI" } ;TI" end ;TI"#text = (1..20).to_a.join(" ") ;TI",enum = wordwrap(text.split(/\s+/), 10) ;TI"puts "-"*10 ;TI"*enum.each { |ws| puts ws.join(" ") } ;TI"puts "-"*10 ;TI"#=> ---------- ;TI"# 1 2 3 4 5 ;TI"# 6 7 8 9 10 ;TI"# 11 12 13 ;TI"# 14 15 16 ;TI"# 17 18 19 ;TI"# 20 ;TI"# ---------- ;T; 0o; ; [I"Dmbox contains series of mails which start with Unix From line. ;TI"BSo each mail can be extracted by slice before Unix From line.;T@o;; [!I"# parse mbox ;TI"open("mbox") { |f| ;TI" f.slice_before { |line| ;TI"" line.start_with? "From " ;TI" }.each { |mail| ;TI" unix_from = mail.shift ;TI" i = mail.index("\n") ;TI" header = mail[0...i] ;TI" body = mail[(i+1)..-1] ;TI"' body.pop if body.last == "\n" ;TI"O fields = header.slice_before { |line| !" \t".include?(line[0]) }.to_a ;TI" p unix_from ;TI" pp fields ;TI" pp body ;TI" } ;TI"} ;TI" ;TI"M# split mails in mbox (slice before Unix From line after an empty line) ;TI"open("mbox") { |f| ;TI"- f.slice_before(emp: true) { |line, h| ;TI" prevemp = h[:emp] ;TI" h[:emp] = line == "\n" ;TI". prevemp && line.start_with?("From ") ;TI" }.each { |mail| ;TI"' mail.pop if mail.last == "\n" ;TI" pp mail ;TI" } ;TI"};T; 0: @fileI"enum.c;T:0@omit_headings_from_table_of_contents_below0I"�enum.slice_before(pattern) -> an_enumerator enum.slice_before { |elt| bool } -> an_enumerator enum.slice_before(initial_state) { |elt, state| bool } -> an_enumerator ;T0[ I"(p1 = v1);T@�FI"Enumerable;TcRDoc::NormalModule00