I believe that this should be an easy problem for someone, while I spent a huge amount of time searching for a solution and could not find the one I like.
I will not try to say in words what I need, but simply give some examples of inputs and their expected results as Rspec code:
Method:
def explode(hash)
...
end
And specification:
describe '#explode' do
it do
expect(explode({:a => 1, :b => 2})).
to eq [[:a, 1, :b, 2]]
end
it do
expect(explode({:a => 1, :b => [2,3,4]})).
to eq [
[:a, 1, :b, 2],
[:a, 1, :b, 3],
[:a, 1, :b, 4]
]
end
it do
expect(explode({:a => [1,2], :b => [3,4]})).
to eq [
[:a, 1, :b, 3],
[:a, 2, :b, 3],
[:a, 1, :b, 4],
[:a, 2, :b, 4]
]
end
it do
expect(explode({:a => 1, :b => [2,3], :c => [4,5,6]})).
to eq [
[:a, 1, :b, 2, :c, 4],
[:a, 1, :b, 3, :c, 4],
[:a, 1, :b, 2, :c, 5],
[:a, 1, :b, 3, :c, 5],
[:a, 1, :b, 2, :c, 6],
[:a, 1, :b, 3, :c, 6]
]
end
end
Solutions in languages other than Ruby are also welcome.