From: Intransition Date: 2009-12-30T05:35:58+09:00 Subject: Re: Using #include at the instance level? On Dec 29, 3:16 pm, Robert Klemme wrote: > 2009/12/29 Intransition : > > > > > This is really becoming annoying. I can't even do it via a dynamic > > class definitions either. > > >  module M > >    def m1; "m1"; end > >    module N > >      def self.n1; "n1"; end > >    end > >  end > > >  class X > >    class << self > >      alias _new new > >      def new(&block) > >        klass = Class.new(self) > >        klass.class_eval(&block) > >        klass._new > >      end > >    end > > >    def m ; m1 ; end > >    def n ; N.n1 ; end > >  end > > >  x = X.new do > >    include M > >  end > > >  p x.m > >  p x.n   #=> uninitialized constant X::N (NameError) > > > Did constant lookup change between 1.8.6 and 1.8.7? Seems to me this > > used to be possible, and I find it unacceptable that normal constant > > lookup would not apply to dynamic class definitions, let alone the > > singleton classes. > > AFAIK it hasn't changed and your code could never work in any version > of Ruby because the const lookup in method #n is done _statically_. > Apart from that you are defining method X#n and not #n > so the lookup could never work if you would instantiate X. > > Also, on a more abstract level: it is at least a bit odd to define > class X which can only ever work if you make sure you include "N" > (whichever way). > > I believe you haven't yet fully analyzed the problem you want to > solve.  I suggest to put it to sleep for a while and then get back > later to it.  I can't help you any more because to me it is not clear > what you are trying to achieve.  So far we only went through technical > issues but the problem you are trying to solve isn't cleat to me yet. All I am trying to do is emulate test/unit but using a DSL. Essentially: class MyTest < Test::Unit::TestCase include M def test_N_n1 assert_equal(N.n1, "n1") end end Becomes: testcase :MyTest do include M testunit :N_n1 do assert_equal(N.n1, "n1") end end That's it. But I can't do it exactly b/c I can't make the include work.