# Unit testing an each function

**URL:** <https://rubytalk.org/t/unit-testing-an-each-function/21641>\
**Category:** ruby-talk\
**Created:** [18 October 2005 09:43 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641 "2005-10-18T09:43:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [18 October 2005 09:43 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/1 "2005-10-18T09:43:40Z")

</div>

I am testing a new class I have written that has an each method, how do I write a test for this?

Build an array from the each and compare it to another array with the correct data in it or is there a cleaner way of doing this?

The plus point here is that the results of the each will be a plain integer sequence.

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [18 October 2005 13:13 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/2 "2005-10-18T13:13:18Z")

</div>

I've done it that way before. If you mix in Enumerable you can also use:

assert\_equals(correct\_values, test\_class.to\_a)

Hope that helps.

James Edward Gray II

> **···**
>
> On Oct 18, 2005, at 4:43 AM, Peter Hickman wrote:
> 
> > I am testing a new class I have written that has an each method, how do I write a test for this?
> > 
> > Build an array from the each and compare it to another array with the correct data in it or is there a cleaner way of doing this?

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 13:16 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/3 "2005-10-18T13:16:58Z")

</div>

That sounds fine to me. What's wrong with that method?

Actually, you don't need to build an array with each at all. IIRC,  
Enumerable defines to\_a (I'm assuming you're including that, right?)  
based on each, so you can simply do something like

&nbsp;&nbsp;fibSequence = FibbonacciSequence.new(9)  
&nbsp;&nbsp;reference = [1, 1, 2, 3, 5, 8 ,13, 21, 34]  
&nbsp;&nbsp;assert\_equal reference, fibSequence.to\_a

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [18 October 2005 13:29 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/4 "2005-10-18T13:29:59Z")

</div>

Kevin Ballard wrote:

> That sounds fine to me. What's wrong with that method?

At the moment the source for the tests looks like poetry, very terse and clean, where everything is a bold statement that does something. Then building up arrays of expected results and collecting the output to be tested looks very clumsy in the middle of the elegant tests. It's more of an aesthetic thing, if it looks 'ugly' (and code in ruby tends to look beautiful) then I start to wonder if perhaps I could be doing this differently.

Pity you cant seem to do @data.each.each {|i| yield i} as it seems a perfect one-liner to me.

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 14:42 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/5 "2005-10-18T14:42:01Z")

</div>

Peter Hickman wrote:

> Pity you cant seem to do @data.each.each {|i| yield i} as it seems a  
> perfect one-liner to me.

I'm not quite sure what you would want that line to do. Can you give an  
example of less terse, actually functional code that illustrates your  
desire?

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [18 October 2005 15:04 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/6 "2005-10-18T15:04:38Z")

</div>

Kevin Ballard wrote:

> Peter Hickman wrote:
> 
> > Pity you cant seem to do @data.each.each {|i| yield i} as it seems a  
> > perfect one-liner to me.  
> > &nbsp;&nbsp;&nbsp;  
> > I'm not quite sure what you would want that line to do. Can you give an  
> > example of less terse, actually functional code that illustrates your  
> > desire?

Sort of like this (trivial example)

data = Array.new  
data \<\< (1..5)  
data \<\< (10..20)

data.each{|i| puts i}

1..5  
10..20

# What I want to work is

data.each.each{|i| puts i}

1  
2  
3  
4  
5  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20

# What I have to write is

data.each{|i| i.each{|j| puts j}}

No great hardship I know but I actually expected data.each.each{...} to  
work.

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [18 October 2005 15:14 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/7 "2005-10-18T15:14:27Z")

</div>

If you know there's going to be Enumerable objects in there, perhaps you could redefine each() to handle the nesting, or provide another iterator for this...

James Edward Gray II

> **···**
>
> On Oct 18, 2005, at 10:04 AM, Peter Hickman wrote:
> 
> > # What I want to work is
> > 
> > data.each.each{|i| puts i}

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 15:36 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/8 "2005-10-18T15:36:58Z")

</div>

Peter Hickman wrote:

> Sort of like this (trivial example)
> 
> data = Array.new  
> data \<\< (1..5)  
> data \<\< (10..20)
> 
> data.each{|i| puts i}
> 
> 1..5  
> 10..20
> 
> # What I want to work is
> 
> data.each.each{|i| puts i}
> 
> 1  
> 2  
> 3  
> 4  
> 5  
> 10  
> 11  
> 12  
> 13  
> 14  
> 15  
> 16  
> 17  
> 18  
> 19  
> 20
> 
> # What I have to write is
> 
> data.each{|i| i.each{|j| puts j}}
> 
> No great hardship I know but I actually expected data.each.each{...} to  
> work.

Huh, that's an interesting idea. Lets see if we can get it to work.

class EachProxy \< Object  
&nbsp;&nbsp;instance\_methods.each { |meth| undef\_method(meth) unless meth.to\_s =~  
/^\_\_/ }

&nbsp;&nbsp;def initialize(obj)  
&nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
&nbsp;&nbsp;end

&nbsp;&nbsp;def method\_missing(meth, \*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;@obj.each { |item| item.send meth, \*args, &block }  
&nbsp;&nbsp;end  
end

class Array  
&nbsp;&nbsp;alias :\_\_each\_\_ :each  
&nbsp;&nbsp;def each(&blk)  
&nbsp;&nbsp;&nbsp;&nbsp;if blk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_\_each\_\_(&blk)  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EachProxy.new(self)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Now your example works fine.

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [18 October 2005 15:25 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/9 "2005-10-18T15:25:06Z")

</div>

James Edward Gray II wrote:

> **···**
>
> > On Oct 18, 2005, at 10:04 AM, Peter Hickman wrote:
> > 
> > > # What I want to work is
> > > 
> > > data.each.each{|i| puts i}
> > 
> > If you know there's going to be Enumerable objects in there, perhaps you could redefine each() to handle the nesting, or provide another iterator for this...
> > 
> > James Edward Gray II
> 
> It's no great hardship it is just that I was taken aback when data.each.each{...} didn't work, it seemed like the right thing to type at that point.

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 15:51 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/10 "2005-10-18T15:51:57Z")

</div>

Kevin Ballard wrote:

> class EachProxy \< Object  
> &nbsp;&nbsp;instance\_methods.each { |meth| undef\_method(meth) unless meth.to\_s =~  
> /^\_\_/ }
> 
> &nbsp;&nbsp;def initialize(obj)  
> &nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def method\_missing(meth, \*args, &block)  
> &nbsp;&nbsp;&nbsp;&nbsp;@obj.each { |item| item.send meth, \*args, &block }  
> &nbsp;&nbsp;end  
> end
> 
> class Array  
> &nbsp;&nbsp;alias :\_\_each\_\_ :each  
> &nbsp;&nbsp;def each(&blk)  
> &nbsp;&nbsp;&nbsp;&nbsp;if blk  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_\_each\_\_(&blk)  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EachProxy.new(self)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end

BTW, this also makes things like the following possible:

&nbsp;&nbsp;ary = %w{a list of words goes here}  
&nbsp;&nbsp;ary.each.gsub!(/[aeiou]/, '') # returns the list minus vowels

But of course it's only useful for side-effect functions (like gsub!)  
or functions that take blocks (like each).

Perhaps I should extend collect to work similarly as well? Currently  
collect with no arguments simply returns the array unchanged, but  
returning a similar proxy object might be nicer. It would let you do  
things like:

&nbsp;&nbsp;ary = %w{a list of words goes here}  
&nbsp;&nbsp;ary.collect.length # returns the length of all words

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [18 October 2005 15:29 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/11 "2005-10-18T15:29:46Z")

</div>

I still recommend a change. If you are always meant to iterator two levels deep, fix each(). If it's a common option, add another iterator for it. Make it easy on user code, much of which might just be written by you. 😉

My two cents.

James Edward Gray II

> **···**
>
> On Oct 18, 2005, at 10:25 AM, Peter Hickman wrote:
> 
> > It's no great hardship it is just that I was taken aback when data.each.each{...} didn't work, it seemed like the right thing to type at that point.

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 16:06 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/12 "2005-10-18T16:06:57Z")

</div>

class EnumerableProxy \< Object  
&nbsp;&nbsp;instance\_methods.each { |meth| undef\_method(meth) unless meth.to\_s =~  
/^\_\_/ }

&nbsp;&nbsp;def initialize(obj, meth, \*args)  
&nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
&nbsp;&nbsp;&nbsp;&nbsp;@meth = meth  
&nbsp;&nbsp;&nbsp;&nbsp;@args = args  
&nbsp;&nbsp;end

&nbsp;&nbsp;def method\_missing(meth, \*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;@obj.send(@meth, \*@args) { |item| item.send meth, \*args, &block }  
&nbsp;&nbsp;end  
end

class Array  
&nbsp;&nbsp;alias :\_\_each\_\_ :each  
&nbsp;&nbsp;def each(&blk)  
&nbsp;&nbsp;&nbsp;&nbsp;if blk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_\_each\_\_(&blk)  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EnumerableProxy.new(self, :each)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;remove\_method :collect  
end

module Enumerable  
&nbsp;&nbsp;alias :\_\_collect\_\_ :collect  
&nbsp;&nbsp;def collect(&blk)  
&nbsp;&nbsp;&nbsp;&nbsp;if blk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_\_collect\_\_(&blk)  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EnumerableProxy.new(self, :collect)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

There, now you can do something like:

&nbsp;&nbsp;ary = %w{a long list of words}  
&nbsp;&nbsp;lengths = ary.collect.length

Of course, Array#collect! still behaves as it used to. However, this  
new #collect behaviour is defined in Enumerable so it will work in more  
than just Array (although the #each stuff is on Array alone).

---

<div class="post-metadata">

**Author:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [18 October 2005 16:09 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/13 "2005-10-18T16:09:08Z")

</div>

Thanks for that, quite an education in that code.

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 17:36 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/14 "2005-10-18T17:36:58Z")

</div>

Ok, here's a new version. It now generalizes the wrapping, so it's  
trivial to add it to new enumerable classes:

class EnumerableProxy \< Object  
&nbsp;&nbsp;instance\_methods.each { |meth| undef\_method(meth) unless meth.to\_s =~  
/^\_\_/ }

&nbsp;&nbsp;def initialize(obj, meth, \*args)  
&nbsp;&nbsp;&nbsp;&nbsp;@obj = obj  
&nbsp;&nbsp;&nbsp;&nbsp;@meth = meth  
&nbsp;&nbsp;&nbsp;&nbsp;@args = args  
&nbsp;&nbsp;end

&nbsp;&nbsp;def method\_missing(meth, \*args, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;@obj.send(@meth, \*@args) { |item| item.send meth, \*args, &block }  
&nbsp;&nbsp;end  
end

module Enumerable  
&nbsp;&nbsp;@@meth\_cache = Hash.new  
&nbsp;&nbsp;def self.meth\_cache  
&nbsp;&nbsp;&nbsp;&nbsp;@@meth\_cache  
&nbsp;&nbsp;end

&nbsp;&nbsp;# I'm using a string for module\_eval instead of a block  
&nbsp;&nbsp;# because using a block necessitates using define\_method,  
&nbsp;&nbsp;# and methods created with define\_method can't handle a block  
&nbsp;&nbsp;# (because Proc's can't handle blocks)  
&nbsp;&nbsp;def self.wrap\_meth(klass, meth)  
&nbsp;&nbsp;&nbsp;&nbsp;meth\_sym = meth.to\_sym.inspect  
&nbsp;&nbsp;&nbsp;&nbsp;klass.module\_eval \<\<-END  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meth = instance\_method(#{meth\_sym})  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enumerable.meth\_cache[#{[klass.name](http://klass.name)}] ||= Hash.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enumerable.meth\_cache[#{[klass.name](http://klass.name)}][#{meth\_sym}] = meth  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def #{meth.to\_s}(&blk)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if blk  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meth = Enumerable.meth\_cache[#{[klass.name](http://klass.name)}][#{meth\_sym}]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meth.bind(self).call(&blk)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EnumerableProxy.new(self, #{meth\_sym})  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;END  
&nbsp;&nbsp;end

&nbsp;&nbsp;wrap\_meth self, :collect  
end

Enumerable.wrap\_meth Array, :each  
Enumerable.wrap\_meth Array, :collect  
Enumerable.wrap\_meth Array, :collect!

Unfortunately, due to limitations in Proc (documented in the comment  
above), I had to use the ugly string form of module\_eval.

I also had to use a method cache on Enumerable itself instead of the  
individual classes, becuase when I tried it on the individual classes  
it wasn't working right (seemed to be accessing Enumerable anyway, so I  
was getting cross-class method binding issues).

Be careful when using irb with this, as it the proxy can cause  
confusion (since the proxy forwards #inspect along with everything  
else). Normally calling #collect without a block is basically the same  
as calling #to\_a, so "foo".collect normally returns ["foo"]. However,  
once you've loaded this code, "foo".collect returns an EnumerableProxy  
object, which irb will display as just "foo" because it'll forward the  
#inspect message irb sends on to "foo".

Usage of this is pretty easy. Wrap any methods you want with  
Enumerable.wrap\_method class, :method. Of course it only makes sense on  
enumerable methods. Feel free to wrap any more of Enumerable's  
built-ins, and any classes you want to use #each with you have to wrap  
separately (along with any class-specific implementations of Enumerable  
methods, like Array#collect). Interestingly, this seems to work  
perfectly fine on destructive methods like Array#collect!.

At this point I'm considering wrapping this up in a gem for  
distribution. Any thoughts?

---

<div class="post-metadata">

**Author:** ![Ryan\_Leavengood](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Ryan\_Leavengood](https://rubytalk.org/u/Ryan_Leavengood)\
**Post date:** [18 October 2005 21:37 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/15 "2005-10-18T21:37:39Z")

</div>

It looks interesting, but I think you are replicating the work done by  
Nobu Nakada with the Enumerator class. For example, read this:

[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/11669710e085ca07](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/11669710e085ca07)

But it seems this newer functionality won't be backported to 1.8, so  
your code may be useful in the meantime. I'm also not sure if the  
enumerator version works exactly the same as yours.

Also, while this is cool, I find it somewhat confusing since it  
changes Ruby iterator semantics quite a bit.

Ryan

> **···**
>
> On 10/18/05, Kevin Ballard \<kballard@gmail.com\> wrote:
> 
> > At this point I'm considering wrapping this up in a gem for  
> > distribution. Any thoughts?

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 22:36 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/16 "2005-10-18T22:36:58Z")

</div>

Ryan Leavengood wrote:

> \>  
> \> At this point I'm considering wrapping this up in a gem for  
> \> distribution. Any thoughts?
> 
> It looks interesting, but I think you are replicating the work done by  
> Nobu Nakada with the Enumerator class. For example, read this:
> 
> [http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/11669710e085ca07](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/11669710e085ca07)
> 
> But it seems this newer functionality won't be backported to 1.8, so  
> your code may be useful in the meantime. I'm also not sure if the  
> enumerator version works exactly the same as yours.

Hrm, interesting, but that doesn't really shed much light on what  
Enumerator is going to do. Is it going to be like a built-in Generator,  
or what? Is there another thread that talks about it?

> Also, while this is cool, I find it somewhat confusing since it  
> changes Ruby iterator semantics quite a bit.

Yeah, but it only affects the case where you don't pass a block, and  
that's not very useful in the default functionality (for #each it  
raises an error, for #collect it appears to just be equivalent to  
#to\_a, dunno about the rest but they're all expecting blocks).

The talk about Enumerator does have me concerned, though, because if  
anybody used my code, and the Enumerator thing goes into 2.0, when they  
upgrade from Ruby 1.8 to 2.0 it's gonna break.

> **···**
>
> > On 10/18/05, Kevin Ballard \<kballard@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 22:41 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/17 "2005-10-18T22:41:58Z")

</div>

Kevin Ballard wrote:

> Hrm, interesting, but that doesn't really shed much light on what  
> Enumerator is going to do. Is it going to be like a built-in Generator,  
> or what? Is there another thread that talks about it?

Hrm, I just discovered that Enumerable::Enumerator already exists  
(require 'enumerator'), but I can't find docs on it. ruby-doc.org gives  
me a 404 on the enumerator page, a google search turns up pages in  
Japanese, and I can't find a file it's loaded from (and SCRIPT\_LINES\_\_  
isn't populated when I require 'enumerator') which leads me to believe  
it's written in C and thus I can't RTSL for docs.

Any pointers?

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 22:56 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/18 "2005-10-18T22:56:58Z")

</div>

Kevin Ballard wrote:

> Kevin Ballard wrote:  
> \> Hrm, interesting, but that doesn't really shed much light on what  
> \> Enumerator is going to do. Is it going to be like a built-in Generator,  
> \> or what? Is there another thread that talks about it?
> 
> Hrm, I just discovered that Enumerable::Enumerator already exists  
> (require 'enumerator'), but I can't find docs on it. ruby-doc.org gives  
> me a 404 on the enumerator page, a google search turns up pages in  
> Japanese, and I can't find a file it's loaded from (and SCRIPT\_LINES\_\_  
> isn't populated when I require 'enumerator') which leads me to believe  
> it's written in C and thus I can't RTSL for docs.
> 
> Any pointers?

And again, I reply to myself. I don't know why I didn't think of this  
the first time - Pickaxe has the documentation. And from what I can  
see, Enumerable::Enumerator is basically just a way to enumerate using  
methods other than each (like Hash#each\_key), which means it's not even  
close to the functionality provided by the code I wrote.

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [18 October 2005 23:03 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/19 "2005-10-18T23:03:12Z")

</div>

> Hrm, I just discovered that Enumerable::Enumerator already exists  
> (require 'enumerator'), but I can't find docs on it. ruby-doc.org gives  
> me a 404 on the enumerator page

I believe James Britt said he had a copy of the docs but couldn't see the value in putting them up:

[http://groups.google.com/group/comp.lang.ruby/msg/7502f37a8a2fbf46](http://groups.google.com/group/comp.lang.ruby/msg/7502f37a8a2fbf46)

I filed a complaint, maybe you should add one too. ☹

> Any pointers?

I discussed enumerator in this Ruby Quiz summary:

[http://www.rubyquiz.com/quiz50.html](http://www.rubyquiz.com/quiz50.html)

Then daz was nice enough to point me to the docs:

[http://groups.google.com/group/comp.lang.ruby/msg/46abfe771a1b0033](http://groups.google.com/group/comp.lang.ruby/msg/46abfe771a1b0033)

For what it's worth, I don't believe enumerator is what you built here. The library is used to switch any given iterator to each():

\>\> require "enumerator"  
=\> true  
\>\> str = "Some data..."  
=\> "Some data..."  
\>\> enum = str.enum\_for(:each\_byte)  
=\> #\<Enumerable::Enumerator:0x31e348\>  
\>\> enum.map { |byte| byte.chr }  
=\> ["S", "o", "m", "e", " ", "d", "a", "t", "a", ".", ".", "."]

Hope that helps.

James Edward Gray II

> **···**
>
> On Oct 18, 2005, at 5:41 PM, Kevin Ballard wrote:

---

<div class="post-metadata">

**Author:** ![Kevin\_Ballard](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kevin\_Ballard](https://rubytalk.org/u/Kevin_Ballard)\
**Post date:** [18 October 2005 23:32 UTC](https://rubytalk.org/t/unit-testing-an-each-function/21641/20 "2005-10-18T23:32:02Z")

</div>

James Edward Gray II wrote:

> For what it's worth, I don't believe enumerator is what you built  
> here.

Do you think what I built here is worth packaging up into a gem?

[Next page](https://rubytalk.org/t/unit-testing-an-each-function/21641.md?page=2)
