I have the following issue.
phrase = "I love Chrome and firefox, but I don't like ie."
browsers = ["chrome", "firefox", "ie", "opera"]
def little_parser ( str )
# what's the best way to retrieve all the browsers within phrase?
end
If we use the method little_parser( phrase ), it should return
["chrome", "firefox", "ie"]
If the phrase was:
phrase_2 = "I don't use Opera"
If we run little_parser( phrase_2 ), it should return only:
["opera"]
How do I do that in the simplest way?
You could iterate through the browsers and use str.include? to single out the items which are in the string:
def little_parser(str)
browsers = ["chrome", "firefox", "ie", "opera"]
browsers.select { |browser| str.include?(browser) }
end
def little_parser(str)
str.scan(/\w+/).map(&:downcase) & browsers
end
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