Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 18th, 2013  |  syntax: None  |  size: 0.62 KB  |  views: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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