Skip to content

Instantly share code, notes, and snippets.

@JunichiIto
Last active October 2, 2020 12:11
Show Gist options
  • Save JunichiIto/9914269 to your computer and use it in GitHub Desktop.
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
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