How can you define c method that is accessible from class One and Two?
module MyGem
class One
def a
c
end
end
class Two
def b
c
end
end
end
There is few ways, for example inheritance vs mixins.
Using mixins, you can define a Module and include in your classes.
module MyGem
class One
include Mixin
def a
c
end
end
class Two
include Mixin
def b
c
end
end
module Mixin
def c
...
end
end
end
Related
I'm trying to modify existing ruby code, and ruby is not my first languange. Part of the code is like below:
#someFile1.rb
module A
module B
def somefunction()
end
end
end
class X::Y
include A::B
end
#someFile2.rb
module A
module C
def anotherfunction()
#somefunction() <-- error
end
end
end
class X::Y
include A::C
end
Somehow I can't access method somefunction() in anotherfunction.
How to access method defined in module B in method inside module C? Why it's not working?
Instance methods in modules aren't generally accessible until you mix them into a class and create an object of that class.
module A
module B
def some_method
"foo"
end
end
end
module A
module C
def another_method
some_method
end
end
end
class X
include A::B
include A::C
end
X.new.another_method
# => "foo"
But I'd say it isn't very elegant to have a module that depends upon the fact that some other module has also been mixed into the same object
Class methods in modules, on the other hand, are accessible like this:
module A
module B
def self.somefunction
"foo"
end
end
end
module A
module C
def self.another_function
A::B.somefunction
end
end
end
A::C.another_function
# => "foo"
Assuming you want to call the module functions themselves, you would first need to make them module functions (think static in Java or namespace in C++). Then you can use the :: (namespace resolution) operator. See foo and bar.
If you want to import them into classes, just import both, and both will be visible. See baz and qux.
module A
module B
def self.foo
puts "foo"
end
def baz
puts "baz"
end
end
end
module A
module C
def self.bar
puts "bar"
A::B::foo
end
def qux
puts "qux"
baz
end
end
end
class X
include A::B
include A::C
end
A::C::bar
x = X.new
x.qux
Output:
bar
foo
baz
qux
How do you make class methods defined within a nested series of modules propagate up the module mixin tree?
Consider the following:
module A
def self.included(base)
base.extend(ClassMethods)
end
def foo; end
module ClassMethods
def bar; end
end
end
module B
include A
end
class C
include B
end
puts "B class methods: #{(B.methods-Module.methods).inspect}"
puts "B instance methods #{B.instance_methods.inspect}"
puts "C class methods: #{(C.methods-Class.methods).inspect}"
puts "C instance methods #{(C.instance_methods-Class.instance_methods).inspect}"
Class C does not inherit the class methods defined in A, even though it includes B.
B class methods: [:bar]
B instance methods [:foo]
C class methods: []
C instance methods [:foo]
Is there a neat way of ensuring the the class methods from A are propagated upwards into C (so I could call C.bar)? I'm looking for a nice generic method that doesn't involve specifically calling out and extending C with every inherited module.
Kind regards
Steve
If you don't want to manually extend ClassMethods and you want anyone including B having its class interface extended by ClassMethods you could do that:
module B
include A
def self.included(base)
base.extend(ClassMethods)
end
end
Or you could have B defined as a class and have C inherit from it. I guess it all depends on your design and what exactly you are trying to achieve.
OK - so I figured it out. It is a slight hack, because it depends on the module hierarchy always using the module name ClassMethods to hold any Class Methods.
However, the following works (in A) - it basically detects whether the base module has ClassMethods defined. If so, it adds A's Classmethods to the base ClassMethods. If not it creates a ClassMethods module in the base (cloning A's class methods)
def self.included(base)
extend ClassMethods
if base.kind_of? Module
if base.include? ClassMethods
base::ClassMethods.extend(ClassMethods)
else
base.const_set(:ClassMethods, ClassMethods)
base.module_eval('def self.included(klass);klass.extend(ClassMethods.clone);end')
end
end
end
So I am trying out mixins and some metaprogramming in ruby and can't get this to work for me. I want it to print "Baboon"
module A
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def install_A
include InstanceMethods
end
end
module InstanceMethods
class B
def testB
#What goes here???
A.monkey
end
end
attr_accessor :monkey
def initialize()
#monkey = "baboon"
end
def test
b = B.new
puts b.testB
end
end
end
class Chimp
include A
install_A
end
c = Chimp.new
c.test
B is its own self-contained class. It is not associated or connected with any of the other classes or modules except to say that an instance of class B happens to be created inside of InstanceMethods::test. Declaring class B inside of the definition for module InstanceMethods limits the scope of B such that it's not visible outside of InstanceMethods, but it doesn't connect B and InstanceMethods in any way.
What you need is to make the module's variable visible inside B. You can implement an initialize method for B that takes a parameter, and InstanceMethods::test can use b = B.new(#monkey) to pass the value to B (make sure B::initialize stores the value as an instance variable).
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
Say there are three classes: A, B & C. I want each class to have a class method, say self.foo, that has exactly the same code for A, B & C.
Is it possible to define self.foo in a module and include this module in A, B & C? I tried to do so and got an error message saying that foo is not recognized.
Yep
module Foo
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def some_method
# stuff
end
end
end
One possible note I should add - if the module is going to be ALL class methods - better off just using extend ModuleName in the Model and defining the methods directly in the module instead - rather than having a ClassMethods module inside the Module, a la
module ModuleName
def foo
# stuff
end
end
module Common
def foo
puts 'foo'
end
end
class A
extend Common
end
class B
extend Common
end
class C
extend Common
end
A.foo
Or, you can extend the classes afterwards:
class A
end
class B
end
class C
end
[A, B, C].each do |klass|
klass.extend Common
end
Rails 3 introduced a module named ActiveSupport::Concern which has the goal of simplifying the syntax of modules.
module Foo
extend ActiveSupport::Concern
module ClassMethods
def some_method
# stuff
end
end
end
It allowed us to save a few lines of "boilerplate" code in the module.
This is basic ruby mixin functionality that makes ruby so special.
While extend turns module methods into class methods, include turns module methods into instance methods in the including/extending class or module.
module SomeClassMethods
def a_class_method
'I´m a class method'
end
end
module SomeInstanceMethods
def an_instance_method
'I´m an instance method!'
end
end
class SomeClass
include SomeInstanceMethods
extend SomeClassMethods
end
instance = SomeClass.new
instance.an_instance_method => 'I´m an instance method!'
SomeClass.a_class_method => 'I´m a class method'
Just wanted to extend Oliver's answer
Define Class methods and instance methods together in a module.
module Foo
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def a_class_method
puts "ClassMethod Inside Module"
end
end
def not_a_class_method
puts "Instance method of foo module"
end
end
class FooBar
include Foo
end
FooBar.a_class_method
FooBar.methods.include?(:a_class_method)
FooBar.methods.include?(:not_a_class_method)
fb = FooBar.new
fb.not_a_class_method
fb.methods.include?(:not_a_class_method)
fb.methods.include?(:a_class_method)