Created
June 16, 2012 01:06
-
-
Save pzaich/2939449 to your computer and use it in GitHub Desktop.
Dictionary Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Dictionary | |
| def initialize(entries = {}) | |
| @entries = entries | |
| end | |
| def add(new_entry) | |
| new_entry = { new_entry => nil } if new_entry.is_a?(String) | |
| @entries.merge! new_entry | |
| end | |
| def entries | |
| @entries.sort_by { |k,v| k }.map { |k,v| {k => v} } | |
| end | |
| def find(search_entry) | |
| return {} if search_entry.empty? | |
| @entries.select { |word, definition| word =~ /^#{search_entry}/ } | |
| end | |
| def keywords | |
| @entries.keys.sort | |
| end | |
| def include?(entry_key) | |
| @entries.has_key? entry_key | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment