简化if equals(==)语句

时间:2013-07-21 03:35:55

标签: ruby if-statement conditional

如何简化以下if语句以删除重复?

if user.name == "john" && user.age > 10 or user.name == "wendy" && user.age > 10 or user.name == "mary" && user.age > 10

谢谢!

2 个答案:

答案 0 :(得分:6)

使用Array#include?

if ['john', 'wendy', 'mary'].include?(user.name) && user.age > 10

答案 1 :(得分:0)

require 'set'

target_names = Set.new ["john", "wendy", "mary"]

if target_names.include? user.name and user.age > 10  ...