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

前端 未结 11 1034
清歌不尽
清歌不尽 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:57

    This is what I came up with

    a = [1, 2, 3, 4, 5]
    b = [2, 3, 5]
    c = [3, 9]
    
    irb(main):037:0* (a + b).sort.uniq == a.sort.uniq
    => true
    irb(main):038:0> (a + c).sort.uniq == a.sort.uniq
    => false
    

提交回复
热议问题