Noz
Noz

Reputation: 6346

Ruby Multiple OR Evaluations For String Value

I was wondering if there was a dry way of writing the following in 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"   

Upvotes: 2

Views: 425

Answers (3)

Sully
Sully

Reputation: 14943

ext =~ /(.xlsx$)|(.xls$)|(.ods$)/

irb(main):009:0> '.xlsx' =~ /(.xlsx$)|(.xls$)|(.ods$)/
=> 0
irb(main):010:0> '.xlsa' =~ /(.xlsx$)|(.xls$)|(.ods$)/
=> nil

Upvotes: 1

OscarRyz
OscarRyz

Reputation: 199244

%w(.xlsx .xls .ods).include? ext

Upvotes: 3

Alcides Queiroz
Alcides Queiroz

Reputation: 9576

Yes, there is...

['.xlsx','.xls','.ods'].include? ext

Upvotes: 5

Related Questions