Mixins

M

Marcin Tyman

Hi guys!!!
My question is:

Is any way to define variables in module which can be further used in
mixin? The problem is that module I wanted to include into class has
constant variables defined which are used within function it implements.
I'm looking for simplest way to overwrite the variables from the class
(may change the variables to non constant ones) the same meaning not to
change implementation of functions from the module. See:

module Mod
AnyVar = 10

def func
#the method use AnyVar
end
end

class MyClass
include Mod

def initialize
#read configuration

#how to make the function use new AnyVar value without changing its
implementation and parameters list
func()
end
end


Thanks a lot for any ideas.
 
S

Sandro Paganotti

I don't know if I get the point but this seems work:

module Mod
@anyvar = 10

def func
puts @anyvar
end
end

class MyClass
include Mod

def initialize
#read configuration
@anyvar = 2
func()
end

end

MyClass.new
# => 2
 
D

David A. Black

Hi --

I don't know if I get the point but this seems work:

module Mod
@anyvar = 10

def func
puts @anyvar
end
end

You've got two variables called @anyvar in that code, and they have no
relation to each other.

The first one is an instance variable belonging to the module Mod
(that is, the actual object Mod). The second one represents an
instance variable that will belong to each object that executes the
func method.

So the @anyvar = 10 line is serving no purpose.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!
 
S

Sandro Paganotti

Well, maybe you can try this way...

module Mod

def Mod.included(mod)
mod.class_eval <<-EOS
alias_method :eek:ld_initialize, :initialize
def initialize
@anyvar = 10
old_initialize
end
EOS
end

def func
puts @anyvar
end
end

class MyClass

def initialize
#read configuration
puts @anyvar
@anyvar = 2
func()
end


include Mod
end

MyClass.new
# => 10
# => 2
 
J

Justin Collins

Marcin said:
Hi guys!!!
My question is:

Is any way to define variables in module which can be further used in
mixin? The problem is that module I wanted to include into class has
constant variables defined which are used within function it implements.
I'm looking for simplest way to overwrite the variables from the class
(may change the variables to non constant ones) the same meaning not to
change implementation of functions from the module. See:

module Mod
AnyVar = 10

def func
#the method use AnyVar
end
end

class MyClass
include Mod

def initialize
#read configuration

#how to make the function use new AnyVar value without changing its
implementation and parameters list
func()
end
end


Thanks a lot for any ideas.

I am not sure I quite understand what you are trying to do, but you can
do something like this:

module A
def initialize
super
@var = 1
end
end

class B
include A

def initialize
super
end

def check_var
puts @var
end
end

B.new.check_var #=> 1


The trouble is you need to make sure you call 'super' religiously if you
are including a lot of modules.

-Justin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Mixins 4
Dynamically mixins? 5
Python Mixins 9
Instance Variable in Mixins 1
mixins 2
improvements on mixins 6
composition vs 'leaf-class mixins' (vs class inheritance) 5
mysterious behavior of mixins 4

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top