确定一个数组是否包含在另一个红宝石数组的内容 [英] Determining whether one array contains the contents of another array in ruby

查看:94
本文介绍了确定一个数组是否包含在另一个红宝石数组的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在红宝石,我如何测试一个阵列不仅具有另一个数组的元素,但它们包含在那个特定的顺序?

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]
[1, 5, 8, 2, 3, 4, 5].function_name(correct_combination) # => false
[8, 10, 1, 2, 3, 4, 5, 9].function_name(correct_combination) # => true

我试着用包含,但用于测试是否 [1,2,3] .INCLUDE?(2)是真还是假。

I tried using include, but that is used to test whether [1,2,3].include?(2) is true or not.

推荐答案

您可以使用each_cons方式:

You can use each_cons method:

arr = [1, 2, 3, 4, 5]
[1, 5, 8, 2, 3, 4, 5].each_cons(arr.size).include? arr

在这种情况下,它会为任何元件工作。

In this case it will work for any elements.

这篇关于确定一个数组是否包含在另一个红宝石数组的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆