Z
Zouplaz
Hello, I would like to use ruby modules for the first time. I understand
theirs principle for a basic usage but get quite unsure for the present
situation :
- I want to put some code in a library (railsapp/lib) to
1) lighten the controller that will use it
2) test unit the code on one side, and test unit the controller on
the other side
- So, the lib is like this
1) class BaseModule
2) class SpecificModule < BaseModule
- The controller SomeController
1) creates an instance sm = SpecificModule.new
2) sm.doSomething
I don't like the idea of creating an instance - Because it seems (to me)
too much Java/C++ oriented
Instead I would like
module BaseModule
module SpecificModule
include BaseModule
class SomeController
include SpecificModule
Then, I could call doIt() from the controller
Hence, my question :
- is it 'good design' to use the modules like this ? May I have some
(bad) surprises later ?
- are the modules 'motchable / stubbable' ?
- is it different to unit test modules compared to classes ?
I did few tries with modules (see above) and I have a question more :
why did I need to use the 'self' keyword with self.my_stuff or
self.method_payment ? (without self. the methods are unknow under
SpecificPayment module)
I had a look to the rails code base and it seems that most methods
defined in modules don't use it... In my case it seems that I should use
'self' anywhere...
Any advice ?
Thank for the attention ;-)
module Payment
@@attribut = "payment"
def self.method_payment()
p 'here again'
end
end
module SpecificPayment
include Payment
@@attribut_specific = "foo"
def self.my_stuff()
p 'here'
Payment.method_payment()
end
end
class AClass
include SpecificPayment
def go()
SpecificPayment.my_stuff()
Payment.method_payment()
end
end
a = AClass.new
a.go()
theirs principle for a basic usage but get quite unsure for the present
situation :
- I want to put some code in a library (railsapp/lib) to
1) lighten the controller that will use it
2) test unit the code on one side, and test unit the controller on
the other side
- So, the lib is like this
1) class BaseModule
2) class SpecificModule < BaseModule
- The controller SomeController
1) creates an instance sm = SpecificModule.new
2) sm.doSomething
I don't like the idea of creating an instance - Because it seems (to me)
too much Java/C++ oriented
Instead I would like
module BaseModule
module SpecificModule
include BaseModule
class SomeController
include SpecificModule
Then, I could call doIt() from the controller
Hence, my question :
- is it 'good design' to use the modules like this ? May I have some
(bad) surprises later ?
- are the modules 'motchable / stubbable' ?
- is it different to unit test modules compared to classes ?
I did few tries with modules (see above) and I have a question more :
why did I need to use the 'self' keyword with self.my_stuff or
self.method_payment ? (without self. the methods are unknow under
SpecificPayment module)
I had a look to the rails code base and it seems that most methods
defined in modules don't use it... In my case it seems that I should use
'self' anywhere...
Any advice ?
Thank for the attention ;-)
module Payment
@@attribut = "payment"
def self.method_payment()
p 'here again'
end
end
module SpecificPayment
include Payment
@@attribut_specific = "foo"
def self.my_stuff()
p 'here'
Payment.method_payment()
end
end
class AClass
include SpecificPayment
def go()
SpecificPayment.my_stuff()
Payment.method_payment()
end
end
a = AClass.new
a.go()