Comparing Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • paintballer
    New Member
    • Feb 2008
    • 2

    Comparing Arrays

    I'm a Ruby n00b trying to write a simple app, and I've been trying to find a way to do this for a while, but can't seem to find anything. Here is essentially what I want to do:


    if array contains 1,2,3 or 4,5,6 then
    execute this code


    But the array may have many more numbers in it than that, and they won't be in the same order. All I've been able to find are things where the two arrays have to be exactly alike.
  • improvcornartist
    Recognized Expert Contributor
    • May 2007
    • 303

    #2
    You can use 'include?' as necessary to determine if the array meets the requirements.
    [CODE=ruby]if array.include?( 1)
    #do this[/CODE]

    Comment

    • paintballer
      New Member
      • Feb 2008
      • 2

      #3
      But is there any way to check for multiple values? I realize that I could nest it, but that would be extremely inconvenient.

      Comment

      • improvcornartist
        Recognized Expert Contributor
        • May 2007
        • 303

        #4
        I'm not sure how your array is set up, so I can't really give you a specific answer, but you should be able to use set intersection or array difference.
        [CODE=ruby]#Set Intersection
        if array && [1,2,3] == [1,2,3]
        #array contains 1,2,3, so do this
        end
        [/CODE]

        Comment

        Working...