Sha256: a9741037894e1988c89974597caf43cf27a24d185b3350b4c00ce43193d5fa6a

Contents?: true

Size: 847 Bytes

Versions: 1

Compression:

Stored size: 847 Bytes

Contents

# Objects of this class are returned by methods retrieving translations 
# from Web dictionaries.

module Dict
  class Result
    attr_reader :term, :translations, :examples
    
    def initialize(term)
      @term = term
      @translations = {}
      @examples = {}
    end
    
    def add_translation(term, translation)
      add_result(@translations, term, translation)
    end
    
    def add_example(term, example)
      add_result(@examples, term, example)
    end
    
    def each_translation
      @translations.each_pair do |term,translation|
        yield term, translation
      end
    end
    
    private
    def add_result(hash, key, value)
      if hash.has_key?(key)
        hash[key].push(value)
      else
        hash.merge!({ key => [value] })
      end
      self
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dict-0.3.4 lib/dict/result.rb