Skip to content

Instantly share code, notes, and snippets.

@rjmcdonald83
Created September 9, 2013 18:12
Show Gist options
  • Save rjmcdonald83/6499379 to your computer and use it in GitHub Desktop.
Save rjmcdonald83/6499379 to your computer and use it in GitHub Desktop.
Ruby Solution to Anagram solved with @seeflanigan
class Anagram
attr_reader :source
attr_reader :match_chars
def initialize(source)
@source = source.downcase
@match_chars = self.source.split('').sort
end
def match(candidates)
candidates.reject {|word| duplicate?(word) }.select {|word| same_chars?(word) }
end
private
def duplicate?(word)
word.downcase.eql? source
end
def same_chars?(word)
word.downcase.split('').sort.eql? match_chars
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment