Assuming a module is included, not extended, what's difference between module instance variable and class variable?
I do not see any difference between two.
module M
@foo = 1
def self.foo
@foo
end
end
p M.foo
module M
@@foo = 1
def self.foo
@@foo
end
end
p M.foo
I have been using @ as @@ within a module, and I recently saw other codes are using @@ within a module. Then I thought I might have been using it incorrectly.
Since we cannot instantiate a module, there must be no difference between @ and @@ for a module. Am I wrong?
----------------------- added the following --------------------
To answer some of questions on comments and posts, I also tested the following.
module M
@foo = 1
def self.bar
:bar
end
def baz
:baz
end
end
class C
include M
end
p [:M_instance_variabies, M.instance_variables] # [@foo]
p [:M_bar, M.bar] # :bar
c = C.new
p c.instance_variables
p [:c_instance_variabies, c.instance_variables] # []
p [:c_baz, c.baz] :baz
p [:c_bar, c.bar] # undefined method
When you include a module within a class, module class variables and class methods are not defined in a class.