I am trying to create an accessor for a class instance variable. I am calling the attr_accessor
method from a module which is included in the class. See the code below:
module Persistence
def self.included(mod)
mod.extend ClassMethods
# Add accessor for class instance variable
class << mod
attr_accessor :persistent_data
end
end
module ClassMethods
def X
persistent_data = 'data'
end
end
end
The above code works. However when I change the code which calls attr_accessor
, to this:
mod.instance_eval do
attr_accessor :persistent_data
end
I get NoMethodError: undefined method `persistent_data='
Shouldn't both ways work the same or is my understanding wrong here? I am using REE 1.8.7