如何簡化以下if語句以刪除重複?如果等於(==)語句的簡化
if user.name == "john" && user.age > 10 or user.name == "wendy" && user.age > 10 or user.name == "mary" && user.age > 10
謝謝!
如何簡化以下if語句以刪除重複?如果等於(==)語句的簡化
if user.name == "john" && user.age > 10 or user.name == "wendy" && user.age > 10 or user.name == "mary" && user.age > 10
謝謝!
require 'set'
target_names = Set.new ["john", "wendy", "mary"]
if target_names.include? user.name and user.age > 10 ...
使用''||代替'或'布爾expressions¡。 – tokland
爲了消除&&的重複,你應該使用'(user.name ==「x」|| user.name ==「y」)&& user.age> 10'顯然,@falsetru寫的更好。我剛剛說過,所以你可以在代碼中使用'()'。 –