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

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

    If you want to ignore the order, (as I did when I came across this post), you could use Array.sort and <=> http://ruby-doc.org/core-1.8.7/classes/Array.html#M000316

    a = [1, 2, 3, 4, 5]
    b = [2, 1, 5, 4, 3]
    a.sort <=> b.sort
    

    You then need to check the output value equals 0.

提交回复
热议问题