Repository URL to install this package:
|
Version:
2.2.2-1 ▾
|
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""+obj+, passing +args+ if any.;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)
;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_below0I"zobj.enum_for(method = :each, *args) -> enum
obj.enum_for(method = :each, *args){|*args| block} -> enum;T0[ I"(*args);T@BFI"Object;TcRDoc::NormalClass00