Array Building idiom

Discussion in 'Ruby' started by John-Mason P. Shackelford, Aug 10, 2006.

  1. What other idioms do you use for:

    a=[]; 10.times{a<<callme() }
    Array.new(10).map{ callme() }

    Note: callme() returns a new value with each call.

    Do I understand correctly that the former will be illegal under 1.9.1?
    --
    John-Mason Shackelford

    Software Developer
    Pearson Educational Measurement

    2510 North Dodge St.
    Iowa City, IA 52245
    ph. 319-354-9200x6214

    http://pearsonedmeasurement.com
     
    John-Mason P. Shackelford, Aug 10, 2006
    #1
    1. Advertisements

  2. On 8/11/06, John-Mason P. Shackelford <> wrote:
    > What other idioms do you use for:
    >
    > a=[]; 10.times{a<<callme() }
    > Array.new(10).map{ callme() }
    >
    > Note: callme() returns a new value with each call.
    >
    > Do I understand correctly that the former will be illegal under 1.9.1?


    Illegal on what basis? Also, Array.new takes a block while
    constructing the array, so you needn't use map:

    irb> Array.new(10, rand(100))
    => [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

    irb> Array.new(10) { rand(100) }
    => [74, 59, 97, 59, 65, 12, 64, 16, 2, 59]

    martin
     
    Martin DeMello, Aug 10, 2006
    #2
    1. Advertisements

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. =?Utf-8?B?TWFyYyBDYXN0cmVjaGluaQ==?=

    Building Dataset from Class Array

    =?Utf-8?B?TWFyYyBDYXN0cmVjaGluaQ==?=, Feb 27, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    443
    =?Utf-8?B?TWFyYyBDYXN0cmVjaGluaQ==?=
    Feb 27, 2004
  2. =?Utf-8?B?TWFyYyBDYXN0cmVjaGluaQ==?=

    Building Dataset from Class Array

    =?Utf-8?B?TWFyYyBDYXN0cmVjaGluaQ==?=, Feb 27, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    4,557
    Steven Cheng[MSFT]
    Mar 5, 2004
  3. Steve Jorgensen
    Replies:
    4
    Views:
    519
    Soren Kuula
    Aug 28, 2005
  4. Midex
    Replies:
    24
    Views:
    1,243
  5. Mara Guida

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

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    830
    David RF
    Sep 3, 2009
  6. Tim Shoppa
    Replies:
    45
    Views:
    836
    Darren Dunham
    Dec 23, 2003
  7. Tom
    Replies:
    3
    Views:
    411
    salsablr
    Dec 20, 2004
  8. Tuan  Bui
    Replies:
    14
    Views:
    793
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...