I'm attempting to include a method from a module in a class, and have the method that is included access a class variable of the base class that's doing the including, but it's not working like I assumed. Code:
class Awesome
@@name = "ME!"
end
module MyGem
module Namespace
def typist
puts @@name
end
end
end
if defined? Awesome
Awesome.class_eval do
include MyGem::Namespace
end
end
Awesome.new.typist # Test our new 'injected' instance method!
#=> NameError: uninitialized class variable @@name in MyGem::Namespace
Clearly my understanding of Ruby's include behaviour is wonky, I thought include would incorporate the method(s) into the base class and the execution context would be the base class, but the error message seems to imply the execution context of included methods is in the original method's module.
So how can I achieve what I'd like to do as illustrated in my code snippet? Mind you, I'm more than happy to replace the class variable with any other implementation, such as using instance variables, or however which it takes to make it work :).