Last active
October 2, 2020 12:11
-
-
Save JunichiIto/9914269 to your computer and use it in GitHub Desktop.
Sample implementation for count_by_word. Please see: http://qiita.com/jnchito/items/c4a56046be1096c19b1c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_by_word(string) | |
string | |
.scan(/\w+/) | |
.group_by{|s| s} | |
.map{|word, words| [word, words.size]} | |
.to_h # require Ruby 2.1 or higher | |
end | |
describe "count by word" do | |
it "counts by word" do | |
expect(count_by_word("no ruby no life")).to eq("no" => 2, "life" => 1, "ruby" => 1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment