Sha256: 1acd8de17c18c24b8e1f6b16c6ffc53380780317c1debe5f3bace8effc1f66fa
Contents?: true
Size: 1.07 KB
Versions: 2
Compression:
Stored size: 1.07 KB
Contents
class ImportError < Exception; end module Kernel # abstraction:: only include the methods given by _meths_ # implementation:: includes a duplicate of _mod_ with all uneeded instance methods removed def import(mod, *meths) mod_dup = mod.dup unless meths.empty? # get list of methods to remove module ims = mod.instance_methods.map {|m| m.to_sym} removes = ims - meths if removes.size != (ims.size - meths.size) raise ImportError, "##{(meths - ims).join(' and #')} not found in #{mod}" end mod_dup.module_eval do removes.each { |meth| remove_method meth } end end include mod_dup end end module Foo def foo; 'foo' end def bar; 'bar' end end class Importer import Foo, :bar end Importer.new.bar # => 'bar' class Importer end class Importer2 import Foo # same as import Foo, :foo, :bar end Importer2.new.bar # => 'bar' Importer2.new.foo # => 'foo' module Foo undef_method :foo def bar; fail end end Importer2.new.bar # => 'bar' Importer2.new.foo # => 'foo'
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
module-import-0.2.2 | ./example.rb |
module-import-0.3.0 | ./example.rb |