# Removing a single level of array nesting.

Discussion in 'Ruby' started by Kenosis, Jun 12, 2006.

1. ### KenosisGuest

Greetings Rubyologists,

Suppose we have a simple array of arrays, e.g., [[1], [2], [3]]. What
would be the cleanest way, if it's possible, to remove the outter level
of array nesting such that the result is (a) a list of arrays: [1],
[2], [3], and (b) suitable for passing as a list of arrays to

Ken

Kenosis, Jun 12, 2006

2. ### Robert KlemmeGuest

Kenosis wrote:
> Greetings Rubyologists,
>
> Suppose we have a simple array of arrays, e.g., [[1], [2], [3]]. What
> would be the cleanest way, if it's possible, to remove the outter level
> of array nesting such that the result is (a) a list of arrays: [1],
> [2], [3], and (b) suitable for passing as a list of arrays to

Your array *is* already a "list" of arrays. Maybe you just need the
star operator. Consider

irb(main):001:0> def foo(*args) p args end
=> nil
irb(main):002:0> foo([[1],[2]])
[[[1], [2]]]
=> nil
irb(main):003:0> foo(*[[1],[2]])
[[1], [2]]
=> nil

Kind regards

robert

Robert Klemme, Jun 12, 2006

3. ### KenosisGuest

Thanks Robert - that's what I thought too but my test in irb failed.
Must be that I have to use *array when passing it to GetopLong.

Regards,

Ken

Robert Klemme wrote:
> Kenosis wrote:
> > Greetings Rubyologists,
> >
> > Suppose we have a simple array of arrays, e.g., [[1], [2], [3]]. What
> > would be the cleanest way, if it's possible, to remove the outter level
> > of array nesting such that the result is (a) a list of arrays: [1],
> > [2], [3], and (b) suitable for passing as a list of arrays to

>
> Your array *is* already a "list" of arrays. Maybe you just need the
> star operator. Consider
>
> irb(main):001:0> def foo(*args) p args end
> => nil
> irb(main):002:0> foo([[1],[2]])
> [[[1], [2]]]
> => nil
> irb(main):003:0> foo(*[[1],[2]])
> [[1], [2]]
> => nil
>
> Kind regards
>
> robert

Kenosis, Jun 12, 2006
4. ### Robert KlemmeGuest

Kenosis <> wrote:
> Thanks Robert - that's what I thought too but my test in irb failed.
> Must be that I have to use *array when passing it to GetopLong.

That's what I was insinuating.

robert

Robert Klemme, Jun 12, 2006