Creating an array with incrementing values up to a certain number

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

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

···

--
Aimred - Ruby Development and Consulting
http://www.aimred.com

p (1..100).to_a

···

On Sun, Aug 17, 2008 at 6:07 PM, Brian Ross <p.brian.ross@gmail.com> wrote:

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 + 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.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Hi --

···

On Mon, 18 Aug 2008, Brian Ross wrote:

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 + 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

--
Rails training from David A. Black and Ruby Power and Light:
   Advancing With Rails August 18-21 Edison, NJ *
* Co-taught by D.A. Black and Erik Kastner
See http://www.rubypal.com for details and updates!