基本上,我尝试编写一个if条件,该条件查看数组的所有内容,以判断该条件是否为真。
基本上,我想做的是:
SubScale.all.each do |ss|
if ss.key IN(scales)
execute this code
end
endWhere在数组中缩放,如果ss.key是该数组中的任何条目,我希望执行代码。
发布于 2011-07-27 23:04:12
你在找Array#include?:
scales.include?(ss.key)发布于 2011-07-28 00:12:33
比Array#include?-which要求您按值检查更通用-is Enumerable#any?
SubScale.all.each do |ss|
# Run the code if the value is an exact match
run_code if scales.include?(ss.key)
end
SubScale.all.each do |ss|
# Run the code if the block returns a truthy value
run_code if scales.any?{ |scale| scale.downcase == ss.key.downcase }
endscale_lookup = Hash[ scales.map{ |s| [s,true] } ]
SubScale.all.each do |ss|
run_code if scale_lookup[ss.key]
end发布于 2011-07-27 23:04:41
scales.include?(ss.key)是您所需要的。
https://stackoverflow.com/questions/6846579
复制相似问题