I have a module A:
module A
extend self
attr_accessor :two, :four
ONE = "one"
@two = "two"
@three = "three"
@@four = "four"
@@five = "five"
def six
"six"
end
end
And I require it inside another file:
require 'a'
include A
p ONE # => "one"
p two # => nil
p A.two # => "two"
p three # => error
p four # => nil
p five # => error
p six # "six"
It seems like any class variable either gives me an error or nil unless I specifically scope it with the module name. I thought using include A would prevent that. How do I export these class variables so that I can reference them directly as two instead of having to use A.two?
if you define variables at class/module level then it is a class instance variable not a instance variable. we are setting getter methods with
||=because modules don’t have initialize method;Then you can use methods directly;