https://github.com/endel/occurrence-counter
Ruby utility to count occurrences from standard data types.
https://github.com/endel/occurrence-counter
Last synced: about 1 year ago
JSON representation
Ruby utility to count occurrences from standard data types.
- Host: GitHub
- URL: https://github.com/endel/occurrence-counter
- Owner: endel
- License: mit
- Created: 2011-05-24T02:04:05.000Z (about 15 years ago)
- Default Branch: master
- Last Pushed: 2011-08-07T18:22:07.000Z (almost 15 years ago)
- Last Synced: 2025-04-12T20:09:21.189Z (about 1 year ago)
- Language: Ruby
- Homepage:
- Size: 99.6 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ruby Occurrence Counter
Utility to count occurrences from standard data types in Ruby.
Feel free to improve my source-code.
## Add to your Gemfile
gem "occurrence_counter", :git => "https://github.com/endel/occurrence-counter.git", :branch => "master"
## Examples:
# Counting Array item occurrences
array = [1,1,1,1,1,3,3,1,3,3,3,5,5]
array.count_occurrences
=> {1 => 6, 3 => 5, 5 => 2}
# Counting word occurrences in String
string = "The quick brown fox jumps over the lazy dog. Because the dog is lazy and he doesn't likes the brown color."
string.word_occurrences(:downcase)
=> {"the"=>4, "quick"=>1, "brown"=>2, "fox"=>1, "jumps"=>1, "over"=>1, "lazy"=>2, "dog"=>2, "because"=>1, "is"=>1, "and"=>1, "he"=>1, "doesnt"=>1, "likes"=>1, "color"=>1}
# Using pre-processor
string = "The quick brown fox jumps over the lazy dog. Because the dog is lazy and he doesn't likes the brown color."
string.word_occurrences(lambda {|str|
str = str.downcase
str.length >= 5 ? str : nil
})
=> {"quick"=>1, "brown"=>2, "jumps"=>1, "because"=>1, "doesnt"=>1, "likes"=>1, "color"=>1}