# Creating array

**URL:** <https://www.ruby-forum.com/t/creating-array/180745>\
**Category:** Ruby\
**Created:** [December 28, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/creating-array/180745 "2009-12-28T12:25:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [December 28, 2009, 12:25pm UTC](https://www.ruby-forum.com/t/creating-array/180745/1 "2009-12-28T12:25:53Z")

</div>

Hi.

Is there a short way of creating array of numbers  
with, for example, step of 10, like this:

10,20,30,40,50

By short I mean without going through iteration loop:

array=[]  
(1…5).each do |i|  
array.push i\*10  
end

Thanks  
Haris

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [December 28, 2009, 12:35pm UTC](https://www.ruby-forum.com/t/creating-array/180745/2 "2009-12-28T12:35:14Z")

</div>

Haris Bogdanoviï¿½ wrote:

> Hi.
> 
> Is there a short way of creating array of numbers  
> with, for example, step of 10, like this:
> 
> 10,20,30,40,50
> 
> By short I mean without going through iteration loop:
> 
> array=[]  
> (1…5).each do |i|  
> array.push i\*10  
> end
> 
> Thanks  
> Haris

Take a look at Array.new[1](http://www.ruby-doc.org/ruby-1.9/classes/Array.html#M000684):

irb(main):001:0\> Array.new(5){|i| i \* 10}  
=\> [0, 10, 20, 30, 40]

With a little bit of math you should be able to get your desired result.

Marvin

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [December 28, 2009, 12:37pm UTC](https://www.ruby-forum.com/t/creating-array/180745/3 "2009-12-28T12:37:36Z")

</div>

Hello

> By short I mean without going through iteration loop:
> 
> array=[]  
> (1…5).each do |i|  
> array.push i\*10  
> end

Would you be pleased with something like this ?

> > array = (1…5).collect {|i| i\*10}  
> > =\> [10, 20, 30, 40, 50]

Cheers,

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [December 28, 2009, 12:39pm UTC](https://www.ruby-forum.com/t/creating-array/180745/4 "2009-12-28T12:39:56Z")

</div>

On 28.12.2009 12:25, Haris Bogdanoviæ wrote:

> end

(1…5).map do |i|  
i\*10  
end

This still has an explicit loop (or at least as explicit a loop as yours  
does), but is somewhat more concise.

On 1.8.7+ you can also do 10.step(50,10).to\_a.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [December 28, 2009, 12:41pm UTC](https://www.ruby-forum.com/t/creating-array/180745/5 "2009-12-28T12:41:42Z")

</div>

Hi –

On Mon, 28 Dec 2009, Haris Bogdanovi? wrote:

> (1…5).each do |i|  
> array.push i\*10  
> end

In 1.8.6 you can do:

array = (1…5).map {|i| i \* 10 }

and in 1.9 you can do:

10.step(50,10).to\_a

I can never keep track of which side of the fence 1.8.7 falls on, but  
you can try both.

David

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [December 28, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/creating-array/180745/6 "2009-12-28T19:37:51Z")

</div>

On Mon, Dec 28, 2009 at 12:25 PM, Haris BogdanoviÄ‡ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is there a short way of creating array of numbers  
> with, for example, step of 10, like this:

> > tens = Array.new(5) { |i| i\*10 }  
> > =\> [0, 10, 20, 30, 40]

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [December 28, 2009, 1:29pm UTC](https://www.ruby-forum.com/t/creating-array/180745/7 "2009-12-28T13:29:42Z")

</div>

2009/12/28 David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 
> 
> and in 1.9 you can do:
> 
> 10.step(50,10).to\_a

In fact, the #to\_a might be completely superfluous depending on that  
the OP wants to do with this.

> I can never keep track of which side of the fence 1.8.7 falls on, but  
> you can try both.

🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [December 29, 2009, 9:21am UTC](https://www.ruby-forum.com/t/creating-array/180745/8 "2009-12-29T09:21:47Z")

</div>

Haris Bogdanoviï¿½ wrote:

> Hi.
> 
> Is there a short way of creating array of numbers  
> with, for example, step of 10, like this:
> 
> 10,20,30,40,50

(10…50).step(10).to\_a
