只是想知道,任何更好的方式来写这个:
if key != "a" && key != "b" && key != "c"
...
end
也许连接上述条件?
unless ["a", "b", "c"].include?(key)
# ...
end
case key
when "a", "b", "c"
else
...
end
unless %w(a b c).include?(key)
# ...
end
一种方法是使用包括:
if !%w(a b c).include?(key) then
...
end
if %w( a b c ).exclude?(key)
...
end
从active_support
unless key[/[abc]/]
...
end
...但我更喜欢sawa的可读性答案。