Ruby: How to check if a string contains multiple items?
I'm familiar with Ruby's include?
method for strings, but how can I chec开发者_JAVA技巧k a string for multiple things?
Specifically, I need to check if a string contains "Fwd:" or "FW:" (and should be case insensitive)
Example string would be: "FWD: Your Amazon.com Order Has Shipped"
the_string =~ /fwd:|fw:/i
You could also use something like
%w(fwd: fw:).any? {|str| the_string.downcase.include? str}
Though personally I like the version using the regex better in this case (especially as you have to call downcase in the second one to make it case insensitive).
精彩评论