In a Ruby class, I'd like to store the value of a variable at the time it includes a given module. Below is a contrived example:
module M
def self.included(base)
base.class_eval do
@@inclusion_time = Time.now
def included_at
@@inclusion_time
end
end
end
end
class A
include M
end
sleep 3
class B
include M
end
sleep 3
class C
include M
end
a = A.new
b = B.new
c = C.new
puts a.included_at
puts b.included_at
puts c.included_at
I've tried this any number of ways (attr_accessor, set_constant, etc,) but the end result is always the same. All of the classes have whatever value was set last.
How do I work around this?
module M
def self.included _
@inclusion_time = Time.now
end
def included_at
self.class.instance_eval{@inclusion_time}
end
end