Dictionary class for ruby that allows for checking existence and finding words starting with a given prefix.
Latest commit e7f897c Mar 27, 2016 @mhuggins Added license to gemspec
Failed to load latest commit information.
lib Bump version to 1.1.1 Aug 14, 2014
tasks First commit Mar 24, 2013
.gitignore First commit Mar 24, 2013
.rspec Add coloring to rspec output Aug 14, 2014
.travis.yml Allow ruby-head to fail gracefully on travis. Jul 27, 2013
Gemfile First commit Mar 24, 2013
LICENSE.txt First commit Mar 24, 2013
README.md A description of #prefixes method is added to README. Aug 14, 2014
Rakefile First commit Mar 24, 2013



Build Status Code Climate

ruby-dictionary provides a simple dictionary that allows for checking existence of words and finding a subset of words given a prefix.


Add this line to your application's Gemfile:

gem 'ruby-dictionary'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ruby-dictionary


A dictionary is created by passing an array of strings to the initializer.

dictionary = Dictionary.new(%w(a ab abs absolute absolutes absolutely
                               absolve be bee been bees bend bent best))

Alternatively, words can be read in from a file (raw or gzip compressed) as well.

dictionary = Dictionary.from_file('path/to/uncompressed.txt')
dictionary = Dictionary.from_file('path/to/compressed.txt.gz')

It is assumed that the file contains one word per line. However, a separator can be passed to the method as an optional second parameter if that's not the case.

dictionary = Dictionary.from_file('path/to/uncompressed.txt', ' ')
dictionary = Dictionary.from_file('path/to/compressed.txt.gz', ',')

Once a dictionary is loaded, the #exists? method can be used to determine if a word exists.

dictionary.exists?('bees')       # => true
dictionary.exists?('wasps')      # => false

The #starting_with method returns a sorted array of all words starting with the provided string.

dictionary.starting_with('bee')  # => ["bee", "been", "bees"]
dictionary.starting_with('foo')  # => []

The #prefixes method returns a sorted array of all the words appearing in the beginning of the provided string.

dictionary.prefixes('abstract')  # => ["a", "ab", "abs"]
dictionary.prefixes('bend')      # => ["be", "bend"]

Case Sensitivity

By default, a new Dictionary is case-insensitive, meaning "bee", "Bee", and "BEE" are all considered to be the same, regardless of adding to the dictionary or searching within it.

However, you can choose to use case-sensitive dictionary by passing an optional true parameter to both the #new and #from_file methods.

dictionary = Dictionary.new(%w(Alpha Beta), true)
dictionary.exists?('Alpha')      # => true
dictionary.exists?('alpha')      # => false

dictionary = Dictionary.from_file('restaurants.txt', "\n", true)
dictionary.starting_with('Mc')   # => ["McDonald's"]
dictionary.starting_with('mc')   # => []

Additionally, you can determine whether a dictionary is case-sensitive via the #case_sensitive? method.

dictionary = Dictionary.new([])
dictionary.case_sensitive?       # => false

dictionary = Dictionary.new([], true)
dictionary.case_sensitive?       # => true


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request