Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class A
- def initialize(add_module)
- self.class.send(:include, MM::NN) if add_module
- end
- end
- module MM::NN
- def display
- puts "module method called"
- end
- end
- A.new(true).display #module method called
- A.new(false).display #NoMethodError
- A.new(true).display #module method called
- A.new(false).display #NoMethodError
- A.new(false).display #NoMethodError
- A.new(true).display #module method called
- module TestModule
- def test_method; "ok"; end
- end
- class A
- def initialize(add_module)
- singleton_class.include(TestModule) if add_module
- end
- end
- A.new(true).test_method # module method called
- A.new(false).test_method # NoMethodError
- class A
- def initialize(add_module)
- extend(MM::NN) if add_module
- end
- end
- class A
- def initialize(add_module)
- if add_module
- (class <<self; include MM::NN; end)
- end
- end
- end
- # This class is here because I'm assuming you can't make `MM::NN` a class in its own right. However it probably needs additional data or methods to actually work with the MM::NN methods
- class NN_implementation
- include MM::NN
- end
- class A
- def initialize(use_module)
- # if this class needs access to data or methods from A, you could add `self` as an input arg to the constructor of `NN_implementation`
- @nn = NN_implmenetation.new if use_module
- end
- def has_nn?
- !@nn.blank?
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement