Short question: when calling a method of an object "a" and passing it a block, can I access "a" from within that block?
Example: Let's say I have an array "words" and I first want to remove all the "bad" words from it (determined using a function) and then find the word that is most frequent among those that remained. I can do it in two lines:
temp_words = words.reject{|w| word_is_bad(w)}
puts temp_words.max{|w| temp_words.count(w)}
However, I wish to avoid having to create a new variable called "temp_words" and do everything in one line, like this:
words.reject{|w| word_is_bad(w)}.max{|w| self.count(w)}
even though I believe my intention is clear, the code fails since "self" does not refer to the temp array generated after reject is called, but to the program's main object.
words.max_by {|w| word_is_bad(w) ? -1 : words.count(w) }(side note, I think you probably want max_by rather than max)