Sha256: c24149fbb375da8f03aab4d7a5b4179af13079d5021f2d8359e4737f871120a2

Contents?: true

Size: 706 Bytes

Versions: 2

Compression:

Stored size: 706 Bytes

Contents

# frozen_string_literal: true

class Groupie
  # Group represents a group or category that words can be classified into.
  class Group
    attr_reader :word_counts

    def initialize(name)
      @name = name
      @word_counts = {}
    end

    def words
      @word_counts.keys
    end

    # Add new words to the group.
    def add(*words)
      words.flatten.each do |word|
        add_word(word)
      end
      nil
    end

    alias << add

    # Return the count for a specific +word+.
    def count(word)
      @word_counts[word] || 0
    end

    private

    # Add a single word and count it.
    def add_word(word)
      @word_counts[word] ||= 0
      @word_counts[word] += 1
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
groupie-0.4.1 lib/groupie/group.rb
groupie-0.4.0 lib/groupie/group.rb