# Creating an array with incrementing values up to a certain number

**URL:** <https://www.ruby-forum.com/t/creating-an-array-with-incrementing-values-up-to-a-certain-number/146106>\
**Category:** Ruby\
**Created:** [August 18, 2008, 12:11am UTC](https://www.ruby-forum.com/t/creating-an-array-with-incrementing-values-up-to-a-certain-number/146106 "2008-08-18T00:11:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 18, 2008, 12:11am UTC](https://www.ruby-forum.com/t/creating-an-array-with-incrementing-values-up-to-a-certain-number/146106/1 "2008-08-18T00:11:16Z")

</div>

Hello,

Another basic question, thanks for the help on the last one. I wanted to  
come up with the above described program and came up with this:

# array.rb start

a = [1]  
x = 0  
while x \< 99  
a[(x+1)] = (a[x] + 1)  
x = x + 1  
end  
p a

# array.rb end

This worked fine, but is there a more elegant way of doing this? It  
seems a  
bit clumsy.

Brian

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 18, 2008, 12:25am UTC](https://www.ruby-forum.com/t/creating-an-array-with-incrementing-values-up-to-a-certain-number/146106/2 "2008-08-18T00:25:19Z")

</div>

irb(main):004:0\> i = 0  
=\> 0  
irb(main):005:0\> a = Array.new(99){i+=1}  
=\> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,  
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,  
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,  
54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,  
71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,  
88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

Farrel

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 18, 2008, 12:25am UTC](https://www.ruby-forum.com/t/creating-an-array-with-incrementing-values-up-to-a-certain-number/146106/3 "2008-08-18T00:25:54Z")

</div>

On Sun, Aug 17, 2008 at 6:07 PM, Brian R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> a[(x+1)] = (a + 1)  
> x = x + 1  
> end  
> p a
> 
> # array.rb end
> 
> This worked fine, but is there a more elegant way of doing this? It seems a  
> bit clumsy.

## p (1..100).to\_a

Rick DeNatale

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

---

<div class="post-metadata">

**Author:** ![circlefusion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ea666f/32.png) [@circlefusion](https://www.ruby-forum.com/u/circlefusion)\
**Post date:** [August 18, 2008, 12:29am UTC](https://www.ruby-forum.com/t/creating-an-array-with-incrementing-values-up-to-a-certain-number/146106/4 "2008-08-18T00:29:25Z")

</div>

Hi –

On Mon, 18 Aug 2008, Brian R. wrote:

> a[(x+1)] = (a + 1)  
> x = x + 1  
> end  
> p a
> 
> # array.rb end
> 
> This worked fine, but is there a more elegant way of doing this? It seems a  
> bit clumsy.

In addition to the other answers:

a = [\*1..99]

David
