如何在Ruby中编写“if in”语句
发布时间:2020-12-17 02:38:21 所属栏目:百科 来源:网络整理
导读:我正在寻找像 Python这样的if-in语句用于Ruby. 基本上,如果an_array中有x ?? 这是我正在处理的代码,其中变量“line”是一个数组. def distance(destination,location,line) if destination and location in line puts "You have #{(n.index(destination) - n
我正在寻找像
Python这样的if-in语句用于Ruby.
基本上,如果an_array中有x 这是我正在处理的代码,其中变量“line”是一个数组. def distance(destination,location,line) if destination and location in line puts "You have #{(n.index(destination) - n.index(location)).abs} stops to go" end end 解决方法if line.include?(destination) && line.include?(location) if [destination,location].all?{ |o| line.include?(o) } if ([destination,location] & line).length == 2 第一个是最明确但最不干燥的. 最后一个是最不清楚的,但是当您有多个要检查的项目时最快. (它是O(m n)vs O(m * n).) 我个人使用中间的,除非速度至关重要. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |