Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # created by Huynh Quang Thao
- # on 02/04/2017
- require 'test/unit'
- # ------------------------------------------------------------
- # implementation
- class Class
- def custom_include(module_name)
- module_name.instance_methods.each do |method|
- puts method
- define_method(method) do |*args, &block|
- # my fault. cannot send here. because module cannot be initialized
- module_name.send(method, *args, &block)
- end
- end
- end
- def custom_extend(module_name)
- module_name.methods(false).each do |method|
- define_singleton_method(method) do |*args, &block|
- module_name.send(method, *args, &block)
- end
- end
- end
- end
- # -------------------------------------------------------------
- # Demonstration
- module SimpleModule
- def self.class_hello_world
- puts 'i am a simple module boss'
- end
- def hello_world
- puts 'i am a simple module client'
- end
- end
- class SimpleClass
- custom_include SimpleModule
- custom_extend SimpleModule
- end
- SimpleClass.class_hello_world
- # -----------------------------------------------------------------
- # Testing
- class TestUsing < Test::Unit::TestCase
- def test_custom_extend
- message = SimpleClass.class_hello_world
- assert message == SimpleModule.class_hello_world
- end
- end
- TestUsing.new("test extend/include")
Advertisement
Add Comment
Please, Sign In to add comment