Trying to understand how to write an instance method that I can call on an object (via the Object#my_method notation). I've only been able to get the desired results by passing my object as an argument to the method, but I'd like to understand an alternate way of writing methods.
class Anagram
attr_reader :test_word
def initialize(test_word)
@test_word = test_word.downcase
end
def word_stats(word)
word.downcase.split("").inject(Hash.new(0)) { |h,v| h[v] += 1; h }
end
def match(word_list)
word_list.delete_if { |word| word.downcase == test_word }
word_list.find_all do |word|
word_stats(test_word) == word_stats(word) # <= ** THIS LINE **
end
end
end
I know I'd need to change the word_stats method, but I'm unsure how to rewrite that line in bold so that I could instead do something like this:
test_word.word_stats == word.word_stats
Is this an appropriate place for send ? Is there a way to write the word_stats method so that it requires no argument?
Thanks!