Created
July 25, 2013 15:54
-
-
Save marksim/6081139 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
class Anagram < Struct.new(:base) | |
def match(candidates) | |
candidates.select do |candidate| | |
word.anagram?(candidate) | |
end | |
end | |
def word | |
@word ||= AnagramWord.new(base) | |
end | |
end | |
class AnagramWord < String | |
def anagram?(word) | |
sorted == AnagramWord.new(word).sorted | |
end | |
def sorted | |
chars.sort | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment