2

I am trying to cycle through all possible combinations of values stored in several arrays. To do this, I decided to put the array names into an array, then use permutation to build all possible combinations of the arrays, with the idea being I would then use the array-stored array names in an '.each do'

$arrays = ["$array1", "$array2", "$array3"]

$arrays.permutation(3).each do |val1,val2,val3|
  val1.each do |step|
    ...check
      val2.each do |step2|
        ...check2
          val3.each do |step3|
            ...check3
           end
       end
    end
 end

However, you can't pass in an array name as a string. How can I make it work?

1
  • That you are using global variables for your array names has bad code smell. It would be good to learn how variables are scoped. Commented Mar 19, 2015 at 22:33

1 Answer 1

2

Just reference the arrays directly (instead of using strings that contain their variable names):

$arrays = [$array1, $array2, $array3]
Sign up to request clarification or add additional context in comments.

2 Comments

ha! why didn't I think of that! Thank you!
@WillVonWizzlepig: you're welcome =) Feel free to upvote and/or accept if you think this answer solves it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.