From: Robert Klemme Date: 2010-06-07T18:34:39+09:00 Subject: Re: Singleton classes and Namespce 2010/6/6 Intransition : > Do singleton classes not get the same namespace treatment as normal > classes? I think so. >  module M >    class X; end >  end > >  o = Object.new >  => # > >  (class << o; self; end).class_eval{ include M } >  => # > >  def o.x; X; end This cannot work since X is not statically in scope. >  o.x >  => NameError: uninitialized constant X >          from (irb):6:in `x' >          from (irb):7 > > The "(class << o; self; end).class_eval{ include M }" is effectively > the same as "o.extend M" but I want to emphasize the use of #include. > > It of course works fine if I use an explicit class. > >  class O >    include M >    def x; X; end >  end > >  O.new.x >  => M::X Yes, but you also changed something else: you defined the method inside the class body. So you did not only switch from class to singleton class but you also changed the lookup. It works if you define the method in the class body in the same way as you did for class O: irb(main):001:0> module M irb(main):002:1> class X; end irb(main):003:1> end => nil irb(main):004:0> o = Object.new => # irb(main):005:0> (class << o; self; end).class_eval do irb(main):006:1* include M irb(main):007:1> def x; X; end irb(main):008:1> end => nil irb(main):009:0> o.x => M::X irb(main):010:0> (class << o; self; end).class_eval do irb(main):011:1* def y; X; end irb(main):012:1> end => nil irb(main):013:0> o.y => M::X irb(main):014:0> Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/