# Enumerable Integer? Nah. Integer#times Enumerator!

**URL:** <https://www.ruby-forum.com/t/enumerable-integer-nah-integer-times-enumerator/124357>\
**Category:** Ruby\
**Created:** [December 27, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/enumerable-integer-nah-integer-times-enumerator/124357 "2007-12-27T22:58:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 27, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/enumerable-integer-nah-integer-times-enumerator/124357/1 "2007-12-27T22:58:11Z")

</div>

Just a little FYI.

Not too long ago I noted this suggestion:

class Integer  
alias\_method :each, :times  
include Enumerable  
end

Which is kind of cool in that allows some convenient statements like

10.collect { |i| “No. #{i}” }  
10.select { |i| i % 3 == 0 }

And so forth. This is nice, but, of course, the downside is that this  
clutters up Integer with all these Enumerable methods.

Thankfully, and this is the FYI, Ruby 1.9 has Integer#times returning  
an Enumerator. So most of these same functionality can be had just by  
inserting #times in between:

10.times.collect { |i| “No. #{i}” }  
10.times.select { |i| i % 3 == 0 }

This is great IMHO, so I thought I’d share. My only aside is the  
thought that perhaps there’s still a good reason to add Integer#to\_a –  
to save us the intermediate object of times.to\_a. Or is that too much  
of a YAGNI?

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 28, 2007, 1:51pm UTC](https://www.ruby-forum.com/t/enumerable-integer-nah-integer-times-enumerator/124357/2 "2007-12-28T13:51:25Z")

</div>

On Dec 28, 2007 8:55 AM, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 10.times.collect { |i| “No. #{i}” }  
> 10.times.select { |i| i % 3 == 0 }
> 
> This is great IMHO, so I thought I’d share. My only aside is the  
> thought that perhaps there’s still a good reason to add Integer#to\_a –  
> to save us the intermediate object of times.to\_a. Or is that too much  
> of a YAGNI?

Hi Trans,

Thanks for the FYI. For what it’s worth, I can’t say I’m really a fan  
of 10.times.select–it doesn’t seem very clear to me what’s being  
iterated over. 10.select even less so. I think good ol’  
(0…10).select looks better. _shrug_

Similarly, 10.to\_a returning [0,1,…,9] doesn’t really feel right to  
me. In fact, I’d probably expect 10.to\_a to do the same as Array(10)  
(and changing the latter may well break things).

Just my thoughts.

Regards,  
George.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 28, 2007, 6:02pm UTC](https://www.ruby-forum.com/t/enumerable-integer-nah-integer-times-enumerator/124357/3 "2007-12-28T18:02:47Z")

</div>

2007/12/27, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 10.collect { |i| “No. #{i}” }  
> 10.select { |i| i % 3 == 0 }
> 
> And so forth. This is nice, but, of course, the downside is that this  
> clutters up Integer with all these Enumerable methods.

Well, in the old days (1.8.x) you could do this which does not look  
too different from the 1.9 solution you show below

10.to\_enum(:times).collect { |i| “No. #{i}” }  
10.to\_enum(:times).select { |i| i % 3 == 0 }

> of a YAGNI?  
> I’d rather use 10.times.to\_a instead of 10.to\_a because the latter  
> seems too unobvious to me.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 28, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/enumerable-integer-nah-integer-times-enumerator/124357/4 "2007-12-28T19:30:44Z")

</div>

On Dec 28, 12:01 pm, “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’d rather use 10.times.to\_a instead of 10.to\_a because the latter  
> seems too unobvious to me.

Understandable. But obvious isn’t always the most important factor. A  
straight #to\_a is vastly more efficient.

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 29, 2007, 11:07am UTC](https://www.ruby-forum.com/t/enumerable-integer-nah-integer-times-enumerator/124357/5 "2007-12-29T11:07:20Z")

</div>

On 28.12.2007 19:30, Trans wrote:

> On Dec 28, 12:01 pm, “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I’d rather use 10.times.to\_a instead of 10.to\_a because the latter  
> > seems too unobvious to me.
> 
> Understandable. But obvious isn’t always the most important factor. A  
> straight #to\_a is vastly more efficient.

Good point. I’d like to point out that the overhead completely depends  
on the number where #to\_a is invoked on. For small numbers the overhead  
is likely significant. But for large numbers, especially BigNums I’d  
say the overhead is negligible. But I guess, in reality this would be  
more often used with rather smallish numbers. So yes, this should be  
taken into account.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [December 29, 2007, 2:23pm UTC](https://www.ruby-forum.com/t/enumerable-integer-nah-integer-times-enumerator/124357/6 "2007-12-29T14:23:18Z")

</div>

On Dec 28, 2007 1:30 PM, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Dec 28, 12:01 pm, “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I’d rather use 10.times.to\_a instead of 10.to\_a because the latter  
> > seems too unobvious to me.
> 
> Understandable. But obvious isn’t always the most important factor. A  
> straight #to\_a is vastly more efficient.

Keep in mind that in 1.8

10.to\_a #=\> [10]

which also gives a deprecation warning, because in 1.9 Object#to\_a and  
several other implementations of to\_a have been removed.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
