Razer
Razer

Reputation: 8211

Use String#end_with?() but ignore case

How to determine if string ends with another string regardless of its case?

filename.end_with?(*%w(.ext1 .e2 .extension))

This example only matches if the case matches too. How to match regardless of the case?

Upvotes: 6

Views: 2746

Answers (1)

falsetru
falsetru

Reputation: 369094

Change filename to lowercase and compare with lowercase extensions.

filename.downcase.end_with?(*%w(.ext1 .e2 .extension))

'MAIN.RB'.downcase.end_with?(*%w(.ruby .rb)) # => true

Upvotes: 9

Related Questions