gpt4 book ai didi

ruby 包括?许多

转载 作者:数据小太阳 更新时间:2023-10-29 08:38:01 25 4
gpt4 key购买 nike

[26] pry(main)> [1,2,3,4].any?{|x| [2,5].include?(x)}
=> true

[27] pry(main)> [1,2,3,4].include?(2,5)
ArgumentError: wrong number of arguments(2 for 1)
from (pry):27:in `include?'

[29] pry(main)> [1,2,3,4].include?(2 || 5) # I want this behavior...
=> true

[30] pry(main)> [1,2,3,4].include?(5 || 2) # but that only works because the above expression evaluates to 2... this to 5
=> false

是否有某种快捷方式可以查看数组中是否包含几项内容? .include 让我可以根据单个值测试数组...我似乎想不出一个聪明的方法来检查多个值,除了我原来的 .any? 在顶部。

最佳答案

您可以计算交集并查看结果数组中是否有交集

2.1.1 :013 > ([1,2,3,4] & [1,2]).any?
=> true
2.1.1 :014 > ([1,2,3,4] & [5,6]).any?
=> false
2.1.1 :015 > ([1,2,3,4] & [1,6]).any?
=> true

参见:http://www.ruby-doc.org/core-2.1.1/Array.html#method-i-26

关于 ruby 包括?许多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23071109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com