O
OliverMarchand
Dear Ruby people,
I understand that modules are used for mixins and namespace management.
Now I am confused about module variables. Here is a sample problem:
---------
module MyMod
attr :myvar, true
def MyMod.othervar=(val)
@othervar = val
end
end
MyMod.myvar = 1 ### this doesn't work
### "undefined method `myvar=' for
MyMod:Module (NoMethodError)"
MyMod.othervar = 1 ### this does!
### I know this works, but is not what I want...
class Klass
include MyMod
end
klass = Klass.new
klass.myvar = 1
---------
So it seems to me that the "attr" call does not create the accessor
functions until an object is instanciated. But what do I do if I want
to solely bundle some functions in a module for namespace management
and store some data that is associated with that module? Do I have to
explicitly write the accessors?
cheers,
Oliver
I understand that modules are used for mixins and namespace management.
Now I am confused about module variables. Here is a sample problem:
---------
module MyMod
attr :myvar, true
def MyMod.othervar=(val)
@othervar = val
end
end
MyMod.myvar = 1 ### this doesn't work
### "undefined method `myvar=' for
MyMod:Module (NoMethodError)"
MyMod.othervar = 1 ### this does!
### I know this works, but is not what I want...
class Klass
include MyMod
end
klass = Klass.new
klass.myvar = 1
---------
So it seems to me that the "attr" call does not create the accessor
functions until an object is instanciated. But what do I do if I want
to solely bundle some functions in a module for namespace management
and store some data that is associated with that module? Do I have to
explicitly write the accessors?
cheers,
Oliver