Hi, let me show the following code:
module M
@@var=“M”
def var
@@var
end
def self.var
@@var
end
end
class A
include M
def var= v
@@var=v
end
end
a=A.new
a.var
=> “M”
a.var=“A”
=> “A”
M.var
=> “A”
WHY is @@var replaced within M module? I do know that this behaviour
exists when inheriting classes, so a children class shares the class
variables of its parent class (which is a documented but undesirable
feature of Ruby). But I didn’t expect it to occur using a module as
I’m not inheriting from a class.
In short, don’t use class variables within a module, am I right?
Thanks a lot.
On Jul 29, 2011, at 5:27 AM, Iaki Baz C. wrote:
But I didn’t expect it to occur using a module as
I’m not inheriting from a class.
Including a module inserts that module into the inheritance hierarchy.
class A
end
class B < A
end
module M
end
p B.ancestors
#=> [B, A, Object, Kernel, BasicObject]
class B
include M
end
p B.ancestors
#=> [B, M, A, Object, Kernel, BasicObject]
More details than you’d probably like about this mechanism can be
found in a blog post of mine:
http://carboni.ca/blog/p/Modules-How-Do-They-Work
Michael E.
[email protected]
http://carboni.ca/
2011/7/29 Michael E. [email protected]:
Including a module inserts that module into the inheritance hierarchy.
More details than you’d probably like about this mechanism can be
found in a blog post of mine:
http://carboni.ca/blog/p/Modules-How-Do-They-Work
Great. Thanks a lot for the information.
2011/7/29 7stud – [email protected]:
A class is a module. So the statement should be: don’t use class
variables period.
Right. Is it not planned to change the behaviour of class variables in
Ruby?
“Iñaki Baz C.” [email protected] wrote in post #1013702:
I’m not inheriting from a class.
The components of a class hierarchy are: parent classes, included
modules (which are inserted immediately above the includer), and
singleton classes.
In short, don’t use class variables within a module, am I right?
A class is a module. So the statement should be: don’t use class
variables ever.
On Fri, Jul 29, 2011 at 12:32 PM, Iaki Baz C. [email protected]
wrote:
2011/7/29 7stud – [email protected]:
A class is a module. So the statement should be: don’t use class
variables period.
Right. Is it not planned to change the behaviour of class variables in Ruby?
I have no idea, you might ask at ruby-core. But there is quite a
consensus not to use them. What is your use case?
As much as I can guess from your example code, class instance
variables should do the trick for you.
e.g.
module A
class << self
attr_accessor :a
end
end
HTH
Robert
2011/7/29 Robert D. [email protected]:
I have no idea, you might ask at ruby-core. But there is quite a
consensus not to use them. What is your use case?
As much as I can guess from your example code, class instance
variables should do the trick for you.
Yes, I try to use instance variables (but let’s say “class’s instance
variables” rather than “class instance’s instance variable” XDD).
I was just wondering.
Thanks a lot.