File: //usr/share/ri/3.0.0/system/Object/enum_for-i.ri
U:RDoc::AnyMethod[iI"
enum_for:ETI"Object#enum_for;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"JCreates a new Enumerator which will enumerate by calling +method+ on ;TI"H+obj+, passing +args+ if any. What was _yielded_ by method becomes ;TI"values of enumerator.;To:RDoc::Markup::BlankLine o;
; [I"CIf a block is given, it will be used to calculate the size of ;TI"Ithe enumerator without the need to iterate it (see Enumerator#size).;T@S:RDoc::Markup::Heading:
leveli: textI"
Examples;T@o:RDoc::Markup::Verbatim; [I"str = "xyz"
;TI"
;TI"%enum = str.enum_for(:each_byte)
;TI"enum.each { |b| puts b }
;TI"# => 120
;TI"# => 121
;TI"# => 122
;TI"
;TI";# protect an array from being modified by some_method
;TI"a = [1, 2, 3]
;TI"some_method(a.to_enum)
;TI"
;TI"<# String#split in block form is more memory-effective:
;TI"Uvery_large_string.split("|") { |chunk| return chunk if chunk.include?('DATE') }
;TI"@# This could be rewritten more idiomatically with to_enum:
;TI"Dvery_large_string.to_enum(:split, "|").lazy.grep(/DATE/).first
;T:@format0o;
; [I"=It is typical to call to_enum when defining methods for ;TI"6a generic Enumerable, in case no block is passed.;T@o;
; [I"HHere is such an example, with parameter passing and a sizing block:;T@o;; [I"module Enumerable
;TI"A # a generic method to repeat the values of any enumerable
;TI" def repeat(n)
;TI"; raise ArgumentError, "#{n} is negative!" if n < 0
;TI" unless block_given?
;TI"I return to_enum(__method__, n) do # __method__ is :repeat here
;TI"< sz = size # Call size and multiply by n...
;TI"B sz * n if sz # but return nil if size itself is nil
;TI" end
;TI"
end
;TI" each do |*val|
;TI"" n.times { yield *val }
;TI"
end
;TI" end
;TI" end
;TI"
;TI".%i[hello world].repeat(2) { |w| puts w }
;TI"6 # => Prints 'hello', 'hello', 'world', 'world'
;TI"enum = (1..14).repeat(3)
;TI"> # => returns an Enumerator when called without a block
;TI"%enum.first(4) # => [1, 1, 1, 2]
;TI"enum.size # => 42;T;0:
@fileI"enumerator.c;T:0@omit_headings_from_table_of_contents_below000[ I"(*args);T@HFI"Object;TcRDoc::NormalClass0[@KFI"to_enum;T