module Outer
class Inner; end
class AnotherInner; end
end
class C
import Outer: [:Inner, :AnotherInner]
def f
Inner
end
end
class Class
def import(constants)
@imported_constants =
(@imported_constants || {}).merge Hash[
constants.flat_map { |namespace, names|
[*names].map { |name| [name.to_sym, "#{namespace}::#{name}"] }
}]
end
def const_missing(name)
const_set name, eval(@imported_constants[name] || raise)
end
end
module Outer
class Inner; end
class AnotherInner; end
end
class C
include Outer
def f
Inner
end
end
C.new.f # => Outer::Inner