I have some methods that contain lots of different combinations of the same logic. To clean it up, I would like to define each test just once.
class Sentence < ApplicationRecord
#Gathers options hash for sentence
def options
{
pronoun: subject.pronoun,
...
}
end
#gives auxiliary verb based on sentence options
def aux
third_person = ["he", "she", "it"].include?(options[:pronoun])
aux = "does" if third_person #just an example
...
end
...
This works fine, but I am trying to pull this out of the aux method to use in other methods.
#this works
@@third_person = ["he", "she", "it"].include?("he")
#this says that there is no options method
@@third_person = ["he", "she", "it"].include?(options[:pronoun])
Does anyone know, what I'm missing?
PREFIXES = %w[ he she it ]and then use that array over and over. You could also use a regular expression if there's some ambiguity or performance concerns. Likewise, returning a temporary hash that's used once is very inefficient, especially if the hash never changes.@@variables?freezemethod so it cannot be modified.PREFIXES = %w(he she it).freeze