Removing a single level of array nesting.

K

Kenosis

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
GetoptLong. Thanks in advance for any help you can lend.

Ken
 
R

Robert Klemme

Kenosis said:
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
GetoptLong. Thanks in advance for any help you can lend.

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
 
K

Kenosis

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 said:
Kenosis said:
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
GetoptLong. Thanks in advance for any help you can lend.

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
 
R

Robert Klemme

Kenosis said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top