SHARE
TWEET

Untitled

a guest Feb 18th, 2013 21 Never
  1. module Outer
  2.   class Inner; end
  3.   class AnotherInner; end
  4. end
  5. class C
  6.   import Outer: [:Inner, :AnotherInner]
  7.   def f
  8.     Inner
  9.   end
  10. end
  11.        
  12. class Class
  13.   def import(constants)
  14.     @imported_constants =
  15.       (@imported_constants || {}).merge Hash[
  16.         constants.flat_map { |namespace, names|
  17.           [*names].map { |name| [name.to_sym, "#{namespace}::#{name}"] }
  18.         }]
  19.   end
  20.  
  21.   def const_missing(name)
  22.     const_set name, eval(@imported_constants[name] || raise)
  23.   end
  24. end
  25.        
  26. module Outer
  27.   class Inner; end
  28.   class AnotherInner; end
  29. end
  30.  
  31. class C
  32.   include Outer
  33.  
  34.   def f
  35.     Inner
  36.   end
  37. end
  38.  
  39. C.new.f # => Outer::Inner
RAW Paste Data
Top