insert an array into an array


J

jtprince

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 :)
ar[1,0] = 'chicken' => "chicken"
ar
=> ["one", "chicken", "two", "three"]

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

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

many thanks!
 
Ad

Advertisements

R

Robert Klemme

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 :)
ar[1,0] = 'chicken' => "chicken"
ar
=> ["one", "chicken", "two", "three"]

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

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

many thanks!

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top