Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample implementation for count_by_word. Please see: http://qiita.com/jnchito/items/c4a56046be1096c19b1c
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