From: transfire@... Date: 2006-06-03T03:49:54+09:00 Subject: Another Look at Namespaces Had a "light-bulb" over head moment and threw this together as a simple demonstration. It's prerry neat. THough is stffes from a few problems of course 1) it probaby not very efficient 2) literal notions don't work and 3) it's not dynamic. #3 could be solved if there were a way to capture const_missing relative to a specific module/class (no inheritance chain). #2 requires Ruby itself to look for, say String class relatively instad of absolutely (potentially dnagerous?) of course #1 is just one of this things ;-) class Module def namespace Module::constants.each do |c| tc = eval("::#{c}") next if tc == self begin const_set c, tc.dup rescue begin const_set c, tc.clone rescue #puts "Can't copy #{c}" end end end module_eval %{ #class Object # include self::Kernel #end include Kernel } end end # try it out class X namespace module Kernel module_function def p( x ) puts "#=> #{x.inspect}" end end class String def upcase "#{self}!!!!!" end end def show p self.class.ancestors p "Hello" # literals don't work p String.new("Hello").upcase end end x = X.new x.show