I'm trying to access an instance variable in the methods of a module.
module Filter
def self.included(target)
puts "INCLUDED: #{@var}"
target.instance_variable_set(:@var, @var) # doesn't seem to work
end
def self.var=(var)
@var = var
end
def self.var
@var
end
def hello
@var
end
end
f = Filter
f.var = "hello"
puts "SET: #{f.var}"
class Main
include Filter
end
m = Main.new
puts "HELLO: #{m.hello}"
It produces this output:
ruby test2.rb
SET: hello
INCLUDED: hello
HELLO:
The last line "HELLO:" needs to output "HELLO: hello". How do I initialize the @var instance variable to make this happen?