I have this module, which gets included in a class:
module MyModule
def self.included base
base.extend ClassMethods
end
module ClassMethods
def my_module_method data
include MyModule::InstanceMethods
after_save :my_module_process
attr_accessor :shared_data
shared_data = data
# instance_variable_set :@shared_data, data
end
end
module InstanceMethods
private
def my_module_process
raise self.shared_data.inspect
# raise instance_variable_get(:@shared_data).inspect
end
end
end
I want to use the data (parameter) passed to my_module_method within my_module_process. I've used attr_accessor as well as instance variables, but either of them return nil.
Since you're using rails, your module can be greatly simplified by making it a AS::Concern
The key points here are:
cattr_accessor, which is similar toattr_accessor, but defines class-level methodsself.class.shared_datato access that class-level data from the instances.Usage:
In ruby, it is super-important to know what is
selfat any given moment. This is what defines the methods and instance variables available to you. As an exercise, I offer you to find out, whyuser.namereturns nil here (and how to fix it).