字符串值的Ruby多重OR评估
我想知道是否在Ruby中编写以下内容的方式很简单:
ext == ".xlsx" || ext == ".xls" || ext == ".ods"
我最初的想法是以下似乎没有按预期工作:
ext == ".xlsx" || ".xls" || ".ods"
%w(.xlsx .xls .ods).include? ext
就在这里…
['.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