def anagram? a, b counts = ->(word) { word.each_char.with_object(Hash.new(0)) { |c, h| h[c] += 1 unless c =~ /\s/} } counts[a] == counts[b] end