# Numeric#of

**URL:** <https://www.ruby-forum.com/t/numeric-of/99591>\
**Category:** Ruby\
**Created:** [February 1, 2006, 12:06am UTC](https://www.ruby-forum.com/t/numeric-of/99591 "2006-02-01T00:06:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 12:06am UTC](https://www.ruby-forum.com/t/numeric-of/99591/1 "2006-02-01T00:06:38Z")

</div>

i threw this out before. thought i’d try again:

harp:~ \> irb  
irb(main):001:0\> class Numeric; def of(&b) Array::new(self).map(&b)  
end; end  
=\> nil

irb(main):002:0\> 42.of{ String::new }  
=\> ["", “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”,  
“”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”,  
“”, “”, “”, “”, “”, “”, “”, “”]

regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 12:18am UTC](https://www.ruby-forum.com/t/numeric-of/99591/2 "2006-02-01T00:18:44Z")

</div>

Hi,

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

> i threw this out before. thought i’d try again:
> 
> harp:~ \> irb  
> irb(main):001:0\> class Numeric; def of(&b) Array::new(self).map(&b) end; end  
> =\> nil
> 
> irb(main):002:0\> 42.of{ String::new }  
> =\> [“”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”]

Using my not-yet-committed 1.9, you can type

42.times.collect{String::new}

to get the same result.

matz.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 12:48am UTC](https://www.ruby-forum.com/t/numeric-of/99591/3 "2006-02-01T00:48:54Z")

</div>

On 1/31/06, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 42.times.collect{String::new}
> 
> to get the same result.

Cool!

So does times still work with a block? I’m assuming that it switches  
behavior, being a generator itself if a block is present, and creating  
a generator object (which includes Enumerable) when no block is  
provided.

Eg. (simplified):

class Numeric  
def times( &blk )  
generator = # some code to create the generator  
# that runs from 0 to self-1  
generator.each &blk if block\_given?  
generator  
end  
end

If that’s the case, this is awesome!

Jacob F.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 12:57am UTC](https://www.ruby-forum.com/t/numeric-of/99591/4 "2006-02-01T00:57:55Z")

</div>

All I did was

class Integer  
def times  
if block\_given?  
(0…self).each { |i| yield i }  
else  
(0…self)  
end  
end  
end

3.times.collect { String.new }  
=\> ["", “”, “”]

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 1:06am UTC](https://www.ruby-forum.com/t/numeric-of/99591/5 "2006-02-01T01:06:59Z")

</div>

On 1/31/06, Matthew M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Integer  
> def times  
> if block\_given?  
> (0…self).each { |i| yield i }  
> else  
> (0…self)  
> end  
> end  
> end

Yeah, that’s pretty much the same thing as my pseudo-code if you let  
generator = (0…self) which, given the special implementation of  
Fixnum ranges, is probably the best solution. Note that it Matz  
probably did do it in Fixnum, not Numeric (How would you do something  
Math::PI.times? 🙂 or Integer.

Jacob F.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 2:01am UTC](https://www.ruby-forum.com/t/numeric-of/99591/6 "2006-02-01T02:01:18Z")

</div>

On Wed, 1 Feb 2006, Yukihiro M. wrote:

> > irb(main):002:0\> 42.of{ String::new }  
> > =\> ["", “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”]
> 
> Using my not-yet-committed 1.9, you can type
> 
> 42.times.collect{String::new}
> 
> to get the same result.

great! i’m still partial to the shorter version since

42.times.collect{String::new}

isn’t that much shorter/prettier than my current idiom of

Array::new(42).map{String::new}

but it’s the capability that’s needed so this is good news.

kind regards.

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 3:01am UTC](https://www.ruby-forum.com/t/numeric-of/99591/7 "2006-02-01T03:01:32Z")

</div>

On Wed, 1 Feb 2006, James Edward G. II wrote:

> On Jan 31, 2006, at 6:58 PM, removed\_email\_address@domain.invalid wrote:
> 
> > Array::new(42).map{String::new}
> 
> That can be shortened to:
> 
> > > Array.new(42) { String.new }  
> > > =\> ["", “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”,  
> > > “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”,  
> > > “”, “”, “”, “”, “”]

indeed. my bad.

none still as pretty as ‘42.of’ though! 😉

-a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 2:28am UTC](https://www.ruby-forum.com/t/numeric-of/99591/8 "2006-02-01T02:28:24Z")

</div>

On Jan 31, 2006, at 6:58 PM, removed\_email\_address@domain.invalid wrote:

> Array::new(42).map{String::new}

That can be shortened to:

> > Array.new(42) { String.new }  
> > =\> ["", “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”,  
> > “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”, “”,  
> > “”, “”, “”, “”, “”, “”, “”, “”, “”]

James Edward G. II

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 7:33am UTC](https://www.ruby-forum.com/t/numeric-of/99591/9 "2006-02-01T07:33:10Z")

</div>

On Feb 1, 2006, at 1:26 AM, Yukihiro M. wrote:

> If returns a enumerator object just like other enumerable methods do  
> if no block is given to them.
> 
> matz.

How deep does this rabbit hole go?

Does map return an enumerable without a block? And if so, can we type  
obj.map.map.map… til the end of time?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 7:51am UTC](https://www.ruby-forum.com/t/numeric-of/99591/10 "2006-02-01T07:51:15Z")

</div>

Hi,

On 2/1/06, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How deep does this rabbit hole go?

As deep as you want, and the machine allows

> Does map return an enumerable without a block? And if so, can we type  
> obj.map.map.map… til the end of time?

Yes.

matz.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 7:27am UTC](https://www.ruby-forum.com/t/numeric-of/99591/11 "2006-02-01T07:27:00Z")

</div>

Hi,

On 2/1/06, Jacob F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 1/31/06, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Using my not-yet-committed 1.9, you can type
> > 
> > 42.times.collect{String::new}
> > 
> > to get the same result.
> 
> So does times still work with a block?

Yes.

> I’m assuming that it switches  
> behavior, being a generator itself if a block is present, and creating  
> a generator object (which includes Enumerable) when no block is  
> provided.

If returns a enumerator object just like other enumerable methods do  
if no block is given to them.

matz.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 2, 2006, 12:02am UTC](https://www.ruby-forum.com/t/numeric-of/99591/12 "2006-02-02T00:02:00Z")

</div>

“James Edward G. II” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> James Edward G. II

Thank for these!!

I was looking for a clean (Ruby) way of creating an array of ascending  
integer values, with the final value zero. IE [1, 2, 3, 4, 0]  
i = 0; a = Array.new( 4 ) { i += 1 }.push 0  
does what I want.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 1, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/numeric-of/99591/13 "2006-02-01T17:05:52Z")

</div>

On 1/31/06, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If returns a enumerator object just like other enumerable methods do  
> if no block is given to them.

Sweet! I’ve wanted this sort of enumerator generation for a while. Is  
this new across the board in 1.9, or have I just been stupid in  
assuming it didn’t work when it has this whole time?

Jacob F.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 2, 2006, 12:02am UTC](https://www.ruby-forum.com/t/numeric-of/99591/14 "2006-02-02T00:02:00Z")

</div>

“James Edward G. II” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> James Edward G. II

Thank for these!!

I was looking for a clean (Ruby) way of creating an array of ascending  
integer values, with the final value zero. IE [1, 2, 3, 4, 0]  
i = 0; a = Array.new( 4 ) { i += 1 }.push 0  
does what I want.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 2, 2006, 12:11am UTC](https://www.ruby-forum.com/t/numeric-of/99591/15 "2006-02-02T00:11:10Z")

</div>

removed\_email\_address@domain.invalid wrote:

> i threw this out before. thought i’d try again:
> 
> harp:~ \> irb  
> irb(main):001:0\> class Numeric; def of(&b) Array::new(self).map(&b) end; end  
> =\> nil
> 
> irb(main):002:0\> 42.of{ String::new }  
> =\> ["", “”, “”, “”, “”, “”, “”, “”, “”, “”, . . .

class Fixnum  
def of  
(0…self).map{ yield }  
end  
end

p 3.of { “” }

===\> ["", “”, “”]

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 2, 2006, 12:29am UTC](https://www.ruby-forum.com/t/numeric-of/99591/16 "2006-02-02T00:29:31Z")

</div>

Phil D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I was looking for a clean (Ruby) way of creating an array of ascending  
> integer values, with the final value zero. IE [1, 2, 3, 4, 0]  
> i = 0; a = Array.new( 4 ) { i += 1 }.push 0  
> does what I want.

(1…4).to\_a.push(0)

also Array.new passes in the index to the block, so you can do

Array.new(4) {|i| i+1}

martin

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 2, 2006, 12:14am UTC](https://www.ruby-forum.com/t/numeric-of/99591/17 "2006-02-02T00:14:12Z")

</div>

The coming dawn of Role-Oriented Progamming is upon us (such is the way  
of the Functor).

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 2, 2006, 12:32am UTC](https://www.ruby-forum.com/t/numeric-of/99591/18 "2006-02-02T00:32:37Z")

</div>

Robert K. wrote:

> > 
> 
> =\> [1, 2, 3, 4, 0]
> 
> > > a=(1…5).inject([]) {|ar,i| ar \<\< (i%5)}  
> > > =\> [1, 2, 3, 4, 0]
> > 
> > > a=(1…5).inject([]) {|ar,i| ar \<\< i} \<\< 0  
> > > =\> [1, 2, 3, 4, 5, 0]

Pardon me, the last one should’ve read:

> > a=(1…4).inject([]) {|ar,i| ar \<\< i} \<\< 0  
> > =\> [1, 2, 3, 4, 0]

```
robert
```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 2, 2006, 12:50am UTC](https://www.ruby-forum.com/t/numeric-of/99591/19 "2006-02-02T00:50:55Z")

</div>

Bah, too long!  
[""] \* 42  
Sorry, just had to jump in 🙂  
.adam sanderson

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [February 2, 2006, 1:33am UTC](https://www.ruby-forum.com/t/numeric-of/99591/20 "2006-02-02T01:33:36Z")

</div>

On 2/1/06, Phil D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I was looking for a clean (Ruby) way of creating an array of ascending  
> integer values, with the final value zero. IE [1, 2, 3, 4, 0]  
> i = 0; a = Array.new( 4 ) { i += 1 }.push 0

a = (1…4).to\_a \<\< 0

-Levin

[Next page](https://www.ruby-forum.com/t/numeric-of/99591.md?page=2)
