检查Ruby中是否有多个条件为真 [英] Check if any of multiple conditions are true in Ruby
本文介绍了检查Ruby中是否有多个条件为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下Ruby条件:
I have the following Ruby conditional:
<% if can? :read, %w(policy journey crash user).map(&:to_sym) %>
我想翻译为:如果用户对数组中的任何资源都具有读取权限.
Which I want to translate to: if the user has read permissions for any of the resources in the array.
但是,它总是返回false.我该如何解决?
However it always returns false. How can I fix it?
我不想做:
if can? :read, :policy || can? :read, :journey || etc...
推荐答案
可以.
可枚举#任何?
正是您要寻找的东西:
Enumerable#any?
is exactly what you're looking for:
<% if %i(policy journey crash user).any? { |action| can? :read, action } %>
仅当可以读取任何操作
时,以上内容才会返回 true
.
The above will return true
only if can read any action
.
注意,我使用了%i
而不是%w
.它为您提供符号数组.
Note, I used %i
instead of %w
. It gives you array of symbols.
这篇关于检查Ruby中是否有多个条件为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文