Convert object structure

Hello, all!

I’m pretty new to Ruby, so this is probably a pretty simple proposition
and I can’t see it. Some help is greatly appreciated!

I’m trying to take an array with the following format:

[ { :name => name1, :uids = [ { :uid => uid1 }, { :uid => uid2 } ] },
:name => name2, :uids = [ { :uid => uid3 }, { :uid => uid4 } ] } ]

and convert it to an array with the following format:

[ { :name => name1, :uid = uid1 }, { :name => name1, :uid = uid2 }, {
:name => name2, :uid = uid3 }, { :name => name2, :uid = uid4 } ]

When I try using collect on the first format to create the second and it
gets close but not quite right. It makes an array of the following
format:

[ [ { :name => name1, :uid = uid1 }, { :name => name1, :uid = uid2 } ],
[ { :name => name2, :uid = uid3 }, { :name => name2, :uid = uid4 } ] ]

But I can’t use this format.

I hope this makes sense. I’m not even sure it makes sense to me! Perhaps
I’m going about it in an entirely wrong way, but I can’t see it.

That works! Thank you very much!

You could just call #flatten on the resulting array.

– Matma R.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs