Flowdock
to_enum(*args) public

Creates a new Enumerator which will enumerate by on calling method on obj.

method

the method to call on obj to generate the enumeration

args

arguments that will be passed in method in addition to the item itself. Note that the number of args must not exceed the number expected by method

Example

str = "xyz"

enum = str.enum_for(:each_byte)
enum.each { |b| puts b }
# => 120
# => 121
# => 122

# protect an array from being modified by some_method
a = [1, 2, 3]
some_method(a.to_enum)
Show source
Register or log in to add new notes.
August 23, 2008
2 thanks

Needs requiring 'enumerator' to work

This method needs that you

require 'enumerator'

for this method to be available.

July 30, 2011 - (>= v1_9_1_378)
0 thanks

now they are built-in

in ruby 1.9 and afterwards, the to_enum and enum_for(synonym for to_enum) methods are buil-in to the language. so there’s no need to require that any more.