Title / Description
Code class Module # BasicObject is used to limit the size of the exposed interface class Instance < BasicObject def initialize(mod, &fixes) # BasicObject doesn't expose #extend and #singleton_class (class << self; self; end).instance_eval do include(mod) instance_eval(&fixes) if block_given? end end end module Namespace # Exposes a module +mod+ under a given +name+. # # +fixes+ is executed under the context of the exposed module and useful # for tweaking the visibility of old modules. def import(name, mod, &fixes) instance = Instance.new(mod, &fixes) define_method(name) { instance } protected(name) end end include Namespace end if __FILE__ == $0 module ExampleNamespace def foo; "foo"; end end class Bar import "ex", ExampleNamespace import "math", Math do public :sqrt end def is_it_working? ex.foo == "foo" && math.sqrt(4) == 2.0 end end b = Bar.new p [:working?, b.is_it_working?] end
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code