Class: Anagrams
- Inherits:
-
Object
- Object
- Anagrams
- Defined in:
- lib/anagrams.rb
Instance Method Summary collapse
- #find_words(seed) ⇒ Object
-
#initialize(dictionary) ⇒ Anagrams
constructor
A new instance of Anagrams.
Constructor Details
#initialize(dictionary) ⇒ Anagrams
Returns a new instance of Anagrams.
11 12 13 14 |
# File 'lib/anagrams.rb', line 11 def initialize(dictionary) @dictionary = dictionary @read_words_from_dictionary = @dictionary.is_a?(Array) ? @dictionary : read_words_from_dictionary end |
Instance Method Details
#find_words(seed) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/anagrams.rb', line 16 def find_words(seed) if seed.index(/\s/) raise InvalidSeedError else @seed = seed split_seed = split_words_to_letters(@seed) split_words = split_anagrams_to_letters(@read_words_from_dictionary) match_word = find_anagrams(split_seed, split_words) result = merge_letters_back_to_words(match_word) result = result.delete_if { |seed| seed == @seed } result end end |