开发者

ruby, evaluation multiple conditions in an array

开发者 https://www.devze.com 2023-01-07 21:37 出处:网络
So maybe I have this all wrong, but I\'m sure there is a way to do this, say I have a an if statement, that I want to return true if all the cond开发者_如何学JAVAitions in an array evaluate as true.

So maybe I have this all wrong, but I'm sure there is a way to do this, say I have a an if statement, that I want to return true if all the cond开发者_如何学JAVAitions in an array evaluate as true.

say I have this:

def real_visitor?(location, request, params)

  valid_location = [
    params['referrer'] == 'us',
    params['bot'] != 'googlebot',
    5 + 5 == 10 
  ]

  if valid_location
    return true
  else
    return false
  end
end

How would I evaluate each of the conditions in the array valid_location, some of those conditions in that array are just pseudocode.


Use Array#any? or Array#all?. It's like putting the || or && operator between all your conditions, but it doesn't do short-circuit evaluation, which is sometimes useful.

return valid_location.all?

You don't need the return keyword, by the way. I'd leave it out.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号