[英]Ruby Multiple OR Evaluations For String Value
I was wondering if there was a dry way of writing the following in Ruby: 我想知道是否在Ruby中编写以下内容的方式很简单:
ext == ".xlsx" || ext == ".xls" || ext == ".ods"
My initial thought was the following which doesn't seem to work as expected: 我最初的想法是以下似乎没有按预期工作:
ext == ".xlsx" || ".xls" || ".ods"
就在这里...
['.xlsx','.xls','.ods'].include? ext
%w(.xlsx .xls .ods).include? ext
ext =~ /(.xlsx$)|(.xls$)|(.ods$)/
irb(main):009:0> '.xlsx' =~ /(.xlsx$)|(.xls$)|(.ods$)/
=> 0
irb(main):010:0> '.xlsa' =~ /(.xlsx$)|(.xls$)|(.ods$)/
=> nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.