insert an array into an array

Discussion in 'Ruby' started by jtprince, Jul 30, 2011.

  1. jtprince

    jtprince Guest

    I need to be able to insert a single element array into another array. Easy, right?
    => ["one", "two", "three"]

    # insertion of elements into an array is easy :)
    => ["one", "chicken", "two", "three"]

    # now, let's insert an array into the array:
    => ["one", "pancake", "chicken", "two", "three"]
    # ??????

    What I need is this after an insertion:
    ["one", ["pancake"], "chicken", "two", "three"]

    many thanks!
    jtprince, Jul 30, 2011
    1. Advertisements

  2. irb(main):001:0> a=%w{a b c}
    => ["a", "b", "c"]
    irb(main):002:0> a[1,0]=[['x']]
    => [["x"]]
    irb(main):003:0> a
    => ["a", ["x"], "b", "c"]

    Kind regards

    Robert Klemme, Jul 30, 2011
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.