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_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
- def custom_include(module_)
- # for public and protected methods:
- module_.instance_methods(include_super=false).each do |meth_name|
- meth = module_.instance_method(meth_name)
- define_method(meth_name) do |*args, &block|
- meth.bind(self).call(*args, &block)
- end
- end
- # for private methods:
- module_.private_instance_methods(include_super=false).each do |meth_name|
- meth = module_.instance_method(meth_name)
- define_method(meth_name) do |*args, &block|
- meth.bind(self).call(*args, &block)
- end
- private :"#{meth_name}"
- end
- end
- def custom_extend(module_)
- singleton_class.custom_include module_
- end
- end
- # -------------------------------------------------------------
- # Demonstration
- module SimpleModule
- def hello_world
- 'Hello World'
- end
- def hello_name
- "Hello to #{@instance_name}"
- end
- def hello_with_name(name)
- res = block_given? ? yield : ''
- "Hello to #{res}.#{name}"
- end
- private
- def private_hello
- 'Hello privately !!!'
- end
- end
- class SimpleClass
- custom_include SimpleModule
- custom_extend SimpleModule
- @instance_name = 'Thao'
- def internal_hello
- private_hello
- end
- end
- puts SimpleClass.hello_world
- puts SimpleClass.hello_name
- puts SimpleClass.new.hello_world
- puts SimpleClass.new.hello_name
- puts SimpleClass.new.hello_with_name('thao')
- # -----------------------------------------------------------------
- # Testing
- class TestUsing < Test::Unit::TestCase
- def test_custom_include_simple
- message = SimpleClass.new.hello_world
- assert message == 'Hello World'
- end
- def test_custom_include_param
- message = SimpleClass.new.hello_with_name('Superman')
- assert message == 'Hello to .Superman'
- end
- def test_custom_include_block
- message = SimpleClass.new.hello_with_name('Superman') do
- 'Mr'
- end
- assert message == 'Hello to Mr.Superman'
- end
- def test_custom_include_private
- assert_raise NoMethodError do
- SimpleClass.new.private_hello
- end
- message = SimpleClass.new.internal_hello
- assert message == 'Hello privately !!!'
- end
- def test_custom_extend_simple
- message = SimpleClass.hello_world
- assert message == 'Hello World'
- end
- def test_custom_extend_shared_scope
- message = SimpleClass.hello_name
- assert message == 'Hello to Thao'
- end
- def custom_extend_param
- message = SimpleClass.hello_with_name('Supergirl')
- assert message == 'Hello to .Supergirl'
- end
- def test_custom_extend_block
- message = SimpleClass.hello_with_name('Supergirl') do
- 'Miss'
- end
- assert message == 'Hello to Miss.Supergirl'
- end
- def test_custom_extend_private
- assert_raise NoMethodError do
- SimpleClass.private_hello
- end
- end
- end
- TestUsing.new("test extend/include")
Advertisement
Add Comment
Please, Sign In to add comment