# Enumerable

**URL:** <https://www.ruby-forum.com/t/enumerable/100866>\
**Category:** Ruby\
**Created:** [August 1, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/enumerable/100866 "2006-08-01T17:42:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [August 1, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/enumerable/100866/1 "2006-08-01T17:42:58Z")

</div>

Am new to Ruby and have the folloiwng Question.

I have a class, say Collect, and I want to mixin Enumerable. How do I  
write the each method in Collect?

class Collect  
include Enumerable  
def initialize()  
@array = Array.new(0)  
@ct = 0  
end  
def add(item)  
@array.push(item)  
@ct += 1  
end  
def howmany  
@ct  
end  
def each  
#  
# Not sure how to do this???  
#  
end  
end

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [August 1, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/enumerable/100866/2 "2006-08-01T18:05:56Z")

</div>

On 8/1/06, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> #
> end
> 
> ```
> 
> end

Assuming you want to iterate over @array,

def each  
@array.each {|i| yield i}  
end

martin

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [August 1, 2006, 6:09pm UTC](https://www.ruby-forum.com/t/enumerable/100866/3 "2006-08-01T18:09:28Z")

</div>

On Tuesday 01 August 2006 16:43, Michael S. wrote:

> end
> 
> # 
> 
> end  
> end  
> It depends what you want each to do? Just to wrap @array.each? In that  
> case,  
> this would work:  
> def each  
> @array.each {|item| yield item}  
> end

Of course you can do whatever you want to the array item in the block  
before  
you yield it again.

An alternative technique is as follows:  
def each(&block)  
@array.each(&block)  
end

I’m sure someone will correct me if I describe this slightly wonky, but  
the  
basic idea is that the interpreter converts the block argument to a proc  
with  
a name (I think I read an article that claimed a block is always  
converted to  
a proc, but when no &arg is given, it is simply a proc with no name,  
accessible of course via yield). This is then passed on to @array.each  
as a  
block argument.

I don’t know whether there are any real advantages or disadvantages to  
either  
method. Hope this helps

Alex

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [August 1, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/enumerable/100866/4 "2006-08-01T18:32:18Z")

</div>

On Tuesday 01 August 2006 17:06, A. S. Bradbury wrote:

> def each(&block)  
> @array.each(&block)  
> end

\> I don't know whether there are any real advantages or disadvantages to \> either method. Hope this helps

Just did a quick benchmark out of curiosity:  
user system total real  
Two yields 15.720000 2.220000 17.940000 ( 20.336562)  
Pass the named block 4.130000 1.020000 5.150000 ( 5.562950)  
Direct on attr\_accessor 3.810000 1.010000 4.820000 ( 5.440259)

I passed the block {|i| i} and initialized @array to (1…100).to\_a, and  
ran  
through 50000 times.

Alex

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [August 1, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/enumerable/100866/5 "2006-08-01T18:22:34Z")

</div>

Michael S. wrote:

> end
> 
> # 
> 
> end  
> end

You have several options:

class Collect  
include Enumerable

def initialize()  
@array = []  
end

def add(item)  
@array.push(item)  
self  
end

alias :\<\< :add

def howmany  
@array.size  
end

# option 1: use yield

def each\_1  
@array.each {|x| yield x}  
self  
end

# option 2: delegate to Array’s each

def each\_2(&b)  
@array.each(&b)  
self  
end  
end

Kind regards

```
robert
```
