I have a data structure that looks like this:
[
{:choices=>["Hello", "Hi"]},
" ",
{:choices=>["wor", {:choices=>["ld", "d"]}, "there"]},
", says ",
"your ",
{:choices=>["friend", "amigo"]}
]
In this structure, choice nodes represent a set of possible values and can be composed.
I need a (probably recursive) Ruby method that will output an array of all of the possible outputs. I.e., for this example:
[
"Hello word, says your friend",
"Hello world, says your friend",
"Hello there, says your friend",
"Hi word, says your friend",
"Hi world, says your friend",
"Hi there, says your friend",
"Hello word, says your amigo",
"Hello world, says your amigo",
"Hello there, says your amigo",
"Hi word, says your amigo",
"Hi world, says your amigo",
"Hi there, says your amigo"
]
I expect this to be recursive, but I’ve been banging my head on it for an hour and I’d like another set of eyes. What am I missing here?
Letting the original data be
a, I think what you wanted is this:But the data that you give is wrong. It does not give what you want:
If you change it to
then you will get: