【发布时间】:2013-12-29 19:06:34
【问题描述】:
我目前正在为 Ruby 做一个 exercism.io,但无法通过最后一次测试。最后的测试显示:
def test_with_apostrophes
phrase = Phrase.new("First: don't laugh. Then: don't cry.")
counts = {"first"=>1, "don't"=>2, "laugh"=>1, "then"=>1, "cry"=>1}
assert_equal counts, phrase.word_count
end
我收到的错误是:
1) Failure:
PhraseTest#test_with_apostrophes [word_count_test.rb:61]:
--- expected
+++ actual
@@ -1 +1 @@
-{"first"=>1, "don't"=>2, "laugh"=>1, "then"=>1, "cry"=>1}
+{"first"=>1, "don"=>2, "t"=>2, "laugh"=>1, "then"=>1, "cry"=>1}
我当前的代码是:
class Phrase
attr_reader :input
def initialize(input)
@input = input
end
def word_count
count = {}
splitted = input.downcase.scan(/\w+/)
splitted.each do | word |
if !count.key?(word)
count[word] = 1
else
count[word] = count[word] + 1
end
end
count
end
end
包含撇号的正则表达式是什么?
【问题讨论】: