Michael Parton Michael Parton - 29 days ago 4
Ruby Question

Can you call an instance method when assigning a class variable in Ruby?

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,

#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

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?


You are not calling the instance method options on instance of class Sentence.

You have to call it this way:

sentence = Sentence.new
['he', 'she', 'it'].include?(sentence.options[:pronoun])