I\'m currently doing an exercism.io for Ruby and can\'t pass the last test. The last test reads :
def test_with_apostrophes
phrase = Phrase.new(\"First: don\'
You want to use a "character class", as described in http://www.regular-expressions.info/charclass.html.
So, instead of \w+
, you can use [\w']+
, which says you want one or more of either a word character or an apostrophe.
Try this for getting your frequency of words,
words_freq = Hash.new(0)
"First: don't laugh. Then: don't cry.".split(/\s+/).each { |word| words_freq[word.downcase.delete(':|.')] += 1 }
gives #words_freq = {"first"=>1, "don't"=>2, "laugh"=>1, "then"=>1, "cry"=>1}
Try:
splitted = input.downcase.scan(/[\w-']+/)