Created
December 16, 2013 15:19
-
-
Save vanmichael/7988700 to your computer and use it in GitHub Desktop.
This file contains 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
require 'pry-debugger' | |
class Anagram | |
attr_reader :word | |
def initialize(word) | |
@word = word | |
end | |
def letters | |
@word.split('') | |
end | |
def anagrams | |
anagrams = [] | |
words = letters.permutation.to_a | |
words.each do |word| | |
anagrams << word.join | |
end | |
anagrams | |
end | |
end | |
anagram = Anagram.new('dog') | |
anagram.letters | |
puts anagram.anagrams |
This file contains 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
require 'rspec' | |
require_relative 'anagram' | |
describe Anagram do | |
it 'generates an array of anagrams' do | |
expect(Anagram.new('dog').anagrams).to include('dgo','odg','ogd','god','gdo') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment