creating array

Discussion in 'Ruby' started by Haris Bogdanoviæ, Dec 28, 2009.

  1. 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
     
    Haris Bogdanoviæ, Dec 28, 2009
    #1
    1. Advertising

  2. Hello

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


    Would you be pleased with something like this ?

    >> array =3D (1..5).collect {|i| i*10}

    =3D> [10, 20, 30, 40, 50]

    Cheers,

    --=20
    JJ Fleck
    PCSI1 Lyc=E9e Kl=E9ber
     
    Fleck Jean-Julien, Dec 28, 2009
    #2
    1. Advertising

  3. 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]:

    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

    [1]: http://www.ruby-doc.org/ruby-1.9/classes/Array.html#M000684
    --
    Posted via http://www.ruby-forum.com/.
     
    Marvin Gülker, Dec 28, 2009
    #3
  4. On 28.12.2009 12:25, Haris Bogdanoviæ wrote:
    > 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
    >


    (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
     
    Sebastian Hungerecker, Dec 28, 2009
    #4
  5. Hi --

    On Mon, 28 Dec 2009, 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


    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

    --
    David A. Black
    Senior Developer, Cyrus Innovation Inc.
    THE COMPLEAT RUBYIST, Ruby training with Black/Brown/McAnally!
    January 22-23, Tampa, Florida
    Info and registration at http://www.thecompleatrubyist.com
     
    David A. Black, Dec 28, 2009
    #5
  6. 2009/12/28 David A. Black <>:
    > Hi --
    >
    > On Mon, 28 Dec 2009, 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=3D[]
    >> (1..5).each do |i|
    >> =A0 array.push i*10
    >> end

    >
    > In 1.8.6 you can do:
    >
    > =A0array =3D (1..5).map {|i| i * 10 }
    >
    > and in 1.9 you can do:
    >
    > =A010.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

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Dec 28, 2009
    #6
  7. David A. Black wrote:

    > Hi --
    >
    > On Mon, 28 Dec 2009, 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

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


    irb(main):001:0> 10.step(50,10).to_a
    => [10, 20, 30, 40, 50]
    irb(main):002:0> RUBY_VERSION
    => "1.8.7"

    --
     
    William James, Dec 28, 2009
    #7
  8. Haris Bogdanoviæ

    Michael Kohl Guest

    On Mon, Dec 28, 2009 at 12:25 PM, Haris Bogdanovi=C4=87 <=
    > wrote:
    > Is there a short way of creating array of numbers
    > with, for example, step of 10, like this:


    >> tens =3D Array.new(5) { |i| i*10 }

    =3D> [0, 10, 20, 30, 40]
     
    Michael Kohl, Dec 28, 2009
    #8
  9. 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Albert Schlef, Dec 29, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravi Ambros Wallau
    Replies:
    0
    Views:
    4,118
    Ravi Ambros Wallau
    Jun 1, 2005
  2. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    521
    David RF
    Sep 3, 2009
  3. Ravi Ambros Wallau
    Replies:
    0
    Views:
    329
    Ravi Ambros Wallau
    Jun 1, 2005
  4. Tom
    Replies:
    3
    Views:
    233
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    522
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page