मैं delegate.rb से एक उदाहरण चोरी करने के लिए जा रहा हूँ, यह प्रतिबंधित करता है यह क्या शामिल
...
class Delegator < BasicObject
kernel = ::Kernel.dup
kernel.class_eval do
[:to_s,:inspect,:=~,:!~,:===,:<=>,:eql?,:hash].each do |m|
undef_method m
end
end
include kernel
...
हो जाता है
module PreciseInclude
def include_except(mod, *except)
the_module = mod.dup
the_module.class_eval do
except.each do |m|
remove_method m # was undef_method, that prevents parent calls
end
end
include the_module
end
end
class Foo
extend PreciseInclude
include_except(SimpleTask, :task1, :task2)
end
Foo.instance_methods.grep(/task/) => [:task3]
आप हमेशा यह फ्लिप कर सकते हैं ताकि बजाय इसे include_only
हो जाता है शामिल
पकड़ यह है कि remove_method नेस्टेड मॉड्यूल के लिए काम नहीं करेगा, और undef का उपयोग करने से उस विधि के लिए पूरे पदानुक्रम को खोजना बंद हो जाएगा।
संभावित डुप्लिकेट [क्या मैं इसे शामिल किए बिना रूबी मॉड्यूल पर एक इंस्टेंस विधि का आह्वान कर सकता हूं?] (Http://stackoverflow.com/questions/322470/can-i-invoke-an-instance-method-on-a -ruby मॉड्यूल-बिना-सहित-यह) –