Determining whether one array contains the contents of another array in ruby

前端 未结 11 1057
清歌不尽
清歌不尽 2021-02-09 03:06

In ruby, how do I test that one array not only has the elements of another array, but contain them in that particular order?

correct_combination = [1, 2, 3, 4, 5         


        
11条回答
  •  我寻月下人不归
    2021-02-09 03:43

    You could simply compare the arrays as strings:

    correct_combination = [1, 2, 3, 4, 5]
    yep = [8, 10, 1, 2, 3, 4, 5, 9]
    nope = [1, 5, 8, 2, 3, 4, 5]
    if yep.to_s.include?(correct_combination.to_s)
      puts "yep"
    end
    if nope.to_s.include?(correct_combination.to_s)
      puts "nope"
    end
    

提交回复
热议问题