# Enumerable#to\_a can run forever

**URL:** <https://www.ruby-forum.com/t/enumerable-to-a-can-run-forever/124119>\
**Category:** Ruby\
**Created:** [December 25, 2007, 12:47am UTC](https://www.ruby-forum.com/t/enumerable-to-a-can-run-forever/124119 "2007-12-25T00:47:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuzb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9c0ed/32.png) [@kuzb](https://www.ruby-forum.com/u/kuzb)\
**Post date:** [December 25, 2007, 12:47am UTC](https://www.ruby-forum.com/t/enumerable-to-a-can-run-forever/124119/1 "2007-12-25T00:47:53Z")

</div>

It’s possible to implement an each method in a class mixing in  
Enumerable that runs forever, and thus causing, for example, to\_a to run  
forever. One such class is mathn’s Prime class:

require ‘mathn’  
Prime.new.to\_a # ← runs forever, never returns

This seems a bit broken to me, and I was writing a program where I  
needed the first n primes, or all the primes under 200, for example, so  
I re-wrote Enumerable#to\_a like so to make my code more idiomatic:

----8\<----  
module Enumerable  
def to\_a(n = nil)  
result = []  
inclusion\_condition = true  
return result if n.to\_i \<= 0 and !block\_given?  
each do |\*elements|  
elements = elements.first if elements.size == 1  
if block\_given?  
values = result.last  
inclusion\_condition = yield(\*values) unless values.nil?  
else  
inclusion\_condition = n != result.size  
end

```
  if inclusion_condition
    result << elements
  else
    result.pop if block_given?
    break result
  end
end
result

```

end  
end  
----8\<----

This allows, for example, the following:  
Prime.new.to\_a(10) # =\> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] , the first  
10 primes  
Prime.new.to\_a {|prime| prime \< 20} # =\> [2, 3, 5, 7, 11, 13, 17, 19] ,  
all the primes under 20

Passed an integer paramater, the code will return an array that long, or  
until the Enumerable runs out. Passed a block, it will return an array  
of elements, adding elements until the block evaluates to false.

This code surely could use some hacking, refactoring, and possibly bug  
fixing. I’ve made a post[1] on [http://refactormycode.com](http://refactormycode.com) just for that  
purpose.

Thoughts?

[1]  
[http://refactormycode.com/codes/196-enumerable-to\_a-with-terminating-conditions](http://refactormycode.com/codes/196-enumerable-to_a-with-terminating-conditions)

## -Ben Kudria

[http://ben.kudria.net](http://ben.kudria.net) | Jabber: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![kuzb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9c0ed/32.png) [@kuzb](https://www.ruby-forum.com/u/kuzb)\
**Post date:** [December 25, 2007, 3:27am UTC](https://www.ruby-forum.com/t/enumerable-to-a-can-run-forever/124119/2 "2007-12-25T03:27:33Z")

</div>

Yukihiro M. wrote:

> Hmm, in 1.9, you have Enumerable#take to work like your to\_i(num), and  
> Enumerable#take\_while like your to\_i{…}.
> 
> p Prime.new.take(10) # =\> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]  
> p Prime.new.take\_while{|prime| prime \< 20} # =\> [2, 3, 5, 7, 11, 13,  
> 17, 19]
> 
> ```
> matz.
> 
> ```

Aaah, ok. Those would be excellent. I look forward to 1.9! Thank you!  
However, what about Prime.new.to\_a ? That would still run forever, and  
the unsuspecting coder might be caught by surprise.

---

<div class="post-metadata">

**Author:** ![kuzb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9c0ed/32.png) [@kuzb](https://www.ruby-forum.com/u/kuzb)\
**Post date:** [December 25, 2007, 3:19am UTC](https://www.ruby-forum.com/t/enumerable-to-a-can-run-forever/124119/3 "2007-12-25T03:19:41Z")

</div>

Hi,

In message “Re: Enumerable#to\_a can run forever”  
on Tue, 25 Dec 2007 08:47:48 +0900, Benjamin K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|require ‘mathn’  
|Prime.new.to\_a # ← runs forever, never returns  
|  
|This seems a bit broken to me, and I was writing a program where I  
|needed the first n primes, or all the primes under 200, for example, so  
|I re-wrote Enumerable#to\_a like so to make my code more idiomatic:

|This allows, for example, the following:  
|Prime.new.to\_a(10) # =\> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] , the first  
|10 primes  
|Prime.new.to\_a {|prime| prime \< 20} # =\> [2, 3, 5, 7, 11, 13, 17, 19] ,  
|all the primes under 20

Hmm, in 1.9, you have Enumerable#take to work like your to\_i(num), and  
Enumerable#take\_while like your to\_i{…}.

p Prime.new.take(10) # =\> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]  
p Prime.new.take\_while{|prime| prime \< 20} # =\> [2, 3, 5, 7, 11, 13,  
17, 19]

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![kuzb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9c0ed/32.png) [@kuzb](https://www.ruby-forum.com/u/kuzb)\
**Post date:** [December 25, 2007, 4:39am UTC](https://www.ruby-forum.com/t/enumerable-to-a-can-run-forever/124119/4 "2007-12-25T04:39:11Z")

</div>

Hi,

In message “Re: Enumerable#to\_a can run forever”  
on Tue, 25 Dec 2007 11:27:29 +0900, Benjamin K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Aaah, ok. Those would be excellent. I look forward to 1.9! Thank you!  
|However, what about Prime.new.to\_a ? That would still run forever, and  
|the unsuspecting coder might be caught by surprise.

It’s no way to avoid entering infinite loop when you do some  
operations on infinite enumerable, such as Prime. For example, trying  
#sort or #max on Prime, or even putting it as a splat will cause  
infinite loop. Adding an argument or a block to #to\_a does not  
improve the situation a lot, I think.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![kuzb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9c0ed/32.png) [@kuzb](https://www.ruby-forum.com/u/kuzb)\
**Post date:** [December 25, 2007, 5:00am UTC](https://www.ruby-forum.com/t/enumerable-to-a-can-run-forever/124119/5 "2007-12-25T05:00:38Z")

</div>

Yukihiro M. wrote:

> It’s no way to avoid entering infinite loop when you do some  
> operations on infinite enumerable, such as Prime. For example, trying  
> #sort or #max on Prime, or even putting it as a splat will cause  
> infinite loop. Adding an argument or a block to #to\_a does not  
> improve the situation a lot, I think.
> 
> ```
> matz.
> 
> ```

Fair enough - but then does it make sense to have these operations  
defined?  
Enumerable seems to mean the same thing as “countable”, but it includes  
methods that apply to only countably finite sets. Perhaps there should  
be a way to distinguish between sets that are countable finite and those  
that are countably infinite? Does 1.9 include provisions for handling  
infinite lists or sets?

## Thanks, Ben Kudria

[http://ben.kudria.net](http://ben.kudria.net) | Jabber: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![kuzb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9c0ed/32.png) [@kuzb](https://www.ruby-forum.com/u/kuzb)\
**Post date:** [December 26, 2007, 1:33am UTC](https://www.ruby-forum.com/t/enumerable-to-a-can-run-forever/124119/6 "2007-12-26T01:33:39Z")

</div>

Yukihiro M. wrote:

> |Fair enough - but then does it make sense to have these operations  
> |defined?
> 
> Because it’s very pragmatically useful for most of the case.  
> Theoretical cleanliness is not my concern.

Aah, I see. I guess I agree.

> |Perhaps there should  
> |be a way to distinguish between sets that are countable finite and those  
> |that are countably infinite?
> 
> And adding one burden to library implementer (including me)?

Fair enough :). It would probably be overly confusing for the coder,  
too.

> |Does 1.9 include provisions for handling  
> |infinite lists or sets?
> 
> I think you mean generator. If so, it has been supported since 1.8.

You are correct, I see Generators in 1.8 can do everything I want.  
Seems I don’t know Ruby as well as I thought. 🙂

Thanks for clarifying everything.

## Ben Kudria

[http://ben.kudria.net](http://ben.kudria.net) | Jabber: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![kuzb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e9c0ed/32.png) [@kuzb](https://www.ruby-forum.com/u/kuzb)\
**Post date:** [December 25, 2007, 5:10am UTC](https://www.ruby-forum.com/t/enumerable-to-a-can-run-forever/124119/7 "2007-12-25T05:10:45Z")

</div>

Hi,

In message “Re: Enumerable#to\_a can run forever”  
on Tue, 25 Dec 2007 13:00:48 +0900, Benjamin K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Fair enough - but then does it make sense to have these operations  
|defined?

Because it’s very pragmatically useful for most of the case.  
Theoretical cleanliness is not my concern.

|Perhaps there should  
|be a way to distinguish between sets that are countable finite and those  
|that are countably infinite?

And adding one burden to library implementer (including me)?

|Does 1.9 include provisions for handling  
|infinite lists or sets?

I think you mean generator. If so, it has been supported since 1.8.

```
          matz.

```
