I am using the Tmail library, and for each attachment in an email, when I do attachment.content_type
, sometimes I get not just the content type but also the name. Examples:
image/jpeg; name=example3.jpg image/jpeg; name=example.jpg image/jpeg; name=photo.JPG image/png
I have an array of valid content types like this:
VALID_CONTENT_TYPES = ['image/jpeg']
I would like to be able to check if the content type is included in any of the valid content types array elements.
What would be the best way of doing so in Ruby?
This is another way to do this: use the Array#index method. It returns the index of the first occurrence of the element in the array. This returns the index of the first word in the array that contains the letter 'o'. index still iterates over the array, it just returns the value of the element.
A substring is a smaller part of a string, it's useful if you only want that specific part, like the beginning, middle, or end. How do you get a substring in Ruby? One way is to use a starting index & a number of characters, inside square brackets, separated by commas.
There are multiple ways to accomplish that. You could check each string until a match is found using Enumerable#any?
:
str = "alo eh tu" ['alo','hola','test'].any? { |word| str.include?(word) }
Though it might be faster to convert the array of strings into a Regexp:
words = ['alo','hola','test'] r = /#{words.join("|")}/ # assuming there are no special chars r === "alo eh tu"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With