I have two modules with the same method name. When I include both modules in some class, only the method of the last module is executed. I need instead both to be executed when I initialize the class:
class MyClass
include FirstModule
include SecondModule
def initialize
foo # foo is contained in both modules but only the one in SecondModules is executed
end
end
Is it doable?
As Yusuke Endoh might say, everything is doable in Ruby. In this case, you have to forget about convenience of just saying 'foo', and you have to be very explicit about what you actually want to do, like this:
class MyClass
include FirstModule
include SecondModule
def initialize
FirstModule.instance_method( :foo ).bind( self ).call
SecondModule.instance_method( :foo ).bind( self ).call
end
end
The line 'FirstModule.instance_method...' can be replaced by simply saying 'foo', but by being explicit, you ensure that no matter what, you are calling the method from that mixin, from which you think you do.
Can you modify the included modules? Perhaps you just call super in the second module?
module M1
def foo
p :M1
end
end
module M2
def foo
p :M2
defined?(super) && super
end
end
class SC
include M1
include M2
def initialize
foo
end
end
SC.new
Or perhaps you actually want to do this?
module M1
def bar; p :M1 end
end
module M2
include M1
def foo; bar; p :M2 end
end
class SC
include M2
def initialize; foo end
end
See live demo here
Related
I have to wrap some behavior around an external gem in a elegant and isolated manner. Given the abstraction below, everything runs smoothly, but 'bar' is never printed.
Could someone tell me why?
My code:
module RefineGem
refine GemMainModule::GemClass do
def self.foo
p 'bar'
super
end
end
end
module Test
using RefineGem
def test
GemMainModule::GemClass.foo
end
end
class Testing
include Test
end
Testing.new.test
Gem code:
module GemMainModule
class Base
include GemMainModule::Fooable
end
class GemClass < Base
end
end
module GemMainModule
module Fooable
extend ActiveSupport::Concern
class_methods do
def foo
p 'zoo'
end
end
end
end
I doubt refinements work for class methods. You might refine the singleton_class though:
module RefineGem
refine GemMainModule::GemClass.singleton_class do
def foo
p 'bar'
super
end
end
end
I personally prefer to use Module#prepend to achieve the same functionality:
GemMainModule::GemClass.singleton_class.prepend(Module.new do
def foo
p 'bar'
super
end
end)
How can I define a method aside a class where I use this method inside the same module?
What I want to do:
module X
def bar
puts 'Hello'
end
class Y
def foo
bar
end
end
end
I want to call bar method using Y.new.foo for example. I want to define methods in my module so that all the classes in this module will inherit them.
Why isn't this working? And what is the closest way to do this?
Assuming that you don't want to include the module in the class, you can't reference the method, as there is no instance to call it on. You can instead:
module X
def self.bar
puts 'Hello'
end
class Y
def foo
X.bar
end
end
end
I think it's better to use a separate object to define shared methods rather than polluting the name space object.
module X
module InstanceMethods
def bar
puts 'Hello'
end
end
class Y
include InstanceMethods
def foo
bar
end
end
end
if you want to use many methods, it's better to use include
module X
def bar
puts 'Hello'
end
class Y
include X
def foo
bar
end
end
end
Consider:
module A
def self.a; puts "a"; end;
def aa; A.a; end;
end
include A
aa
a
aa works but not a. Is there a trick to get A.a available as is a just as include Math makes log available as Math.log? I suspect the way is to write an a method for each self.a method, but is there's a trick to avoid that?
Might you be looking for extend self?
module A
def a; puts "a"; end;
extend self
end
A.a
include A
a
Based on other people's answers, I think what I want is:
module A
module Functions
def a
puts 'a'
end
end
extend Functions
end
# a is a module function of A
A.a
# Explicitly include these functions into one's instance
include A::Functions
a
Now one can include A without polluting their instance space with methods...
unless explicitly doing so with include A::Functions.
There is a trick if you have access to the modules source, and there's still a trick if you don't. If you do, this would be the module A:
module A
def a
puts 'a!'
end
module_function :a
end
All of these will call a:
A.a
include A; a
Even if you don't have access to the module's source, this is still possible with a little (lot) of metaprogramming:
SomeMod.instance_methods(false).each {|method| SomeMod.__send__(:module_function, method.to_sym)}
This only works if the methods are defined as instance methods only in the original module.
If you want do define them as class methods and only make the instance when included:
module A
def self.a
puts 'a'
end
def self.included(klass)
A.singleton_class.instance_methods(false).each do |m|
klass.__send__(:define_method, m.to_sym) do |*args|
A.__send__(m.to_sym, *args)
end
end
end
end
In Ruby, I can do this:
module Foo
end
class Bar
include Foo
end
module Foo
def do_something_instancey
puts "I'm an instance!"
end
end
Then, if I instantiate a Bar object, I can call do_something_instancey on it:
b = Bar.new
b.do_something_instancey
However, if I do this...
module Foo
def self.included(base)
def base.do_something_classy do
puts "I'm a class!"
end
end
end
My understanding is that because I included Foo in Bar before defining that class method, I cannot call Bar.do_something_classy because it never got "attached" to Bar.
I realize that might be slightly inaccurate/not really the right terminology. Regardless, is there a way, in the above example, to attach a class method to Bar from Foo after the module has already been included?
Here's an example for both, class and instance methods:
module Foo
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
end
end
class Bar
include Foo
end
module Foo
def do_something_instancey
puts "I'm an instance!"
end
module ClassMethods
def do_something_classy
puts "I'm a class!"
end
end
end
b = Bar.new
b.do_something_instancey
# => I'm an instance!
Bar.do_something_classy
# => I'm a class!
To add class methods to each class that has (already) included a specific module, you could traverse Ruby's ObjectSpace:
ObjectSpace.each_object(Class) do |klass|
if klass.include? Foo
klass.define_singleton_method(:do_something_classy) do
puts "I'm a class!"
end
end
end
Description of retroactive_module_inclusion gem:
This gem circumvents the "dynamic module include" (aka "double
inclusion") problem, which is the fact that M.module_eval { include N
} does not make the methods of module N available to modules and
classes which had included module M beforehand, only to the ones that
include it thereafter. This behaviour hurts the least surprise
principle, specially because if K is a class, then K.class_eval {
include M } does make all methods of M available to all classes
which had previously inherited it.
possibly I'm not explaining the concept very well, but I'm looking to add class methods to a series of ruby classes to enable them to hold class specific information which will then be called by individual instance methods of the classes.
I can make it work, but it is a bit ugly. Can anyone as it requires 2 modules, one included and the other extended (see example code below).
Can anyone think of a more elegant way of implementing this functionality ?
Thanks
Steve
This module is extended to give class methods but adding an instance member to each class it is included in
module My1
def my_methods (*sym_array)
#my_methods=sym_array
end
def method_list
#my_methods
end
end
This module is included to give instance methods
module My2
def foo
self.class.method_list.each { |m| self.send m }
end
end
Now use the modules - the ugliness is having to use an include and extend statement to allow me to pass a set of symbols to a class method which will then be implemented in an
instance
class Foo
extend My1
include My2
my_methods :baz
def baz
puts "Baz!"
end
end
class Bar
extend My1
include My2
my_methods :frodo
def frodo
puts "Frodo!"
end
end
class Wibble < Bar
extend My1
include My2
my_methods :wobble
def wobble
puts "Wobble!"
end
end
Here is the required output - note that each class has its own instance #my_methods so the behaviour is different for the derived class Wibble < Bar
f=Foo.new
b=Bar.new
w=Wibble.new
f.foo #=> "Bar!"
b.foo #=> "Frodo!"
w.foo #=> "Wobble!"
When a module is included, a hook is called on it. You can use that to do the extend you want.
module M1
def self.included(base)
base.extend(M2)
end
end
People often call that second module M1::ClassMethods. If you're using rails, ActiveSupport::Concern encapsulates this pattern
I would suggest to use a hook from module instead:
module MyModule
def self.included(klass)
klass.extend ClassMethods
end
def foo
self.class.method_list.each{ |m| self.send m }
end
module ClassMethods
attr_reader :method_list
def my_methods(*sym_array)
#method_list = sym_array
end
end
end
So it simplifies to call include only a module whenever you want the functionality to given classes.
class Foo
include MyModule
my_methods :baz
def baz
puts "Baz!"
end
end