From: Robert Klemme Date: 2009-12-30T01:55:36+09:00 Subject: Re: Using #include at the instance level? 2009/12/29 Intransition : > > > On Dec 29, 9:37 am, Robert Klemme wrote: >> 2009/12/29 Intransition : > >> I see to be a bit slow today: how do you want to use modules there? >> Do you want to include them in the test code?  What about: >> >> class TestCase >>   def new(cl, &bl) Btw, that method's name should have read "initialize". >>      @cl = cl >>      instance_eval(&bl) >>   end >> >>   alias include extend >> end >> >> TestCase.new String do >>   extend Foo >>   include Bar >> end > > No no. You got it exactly. Problem is the module's constants are not > coming through the #extend (which is effectively the same as the > include in the singleton class). Let say I have a library: > >  module MyApp >    module SomeSpace >      class FooClass >        ... >       end >    end >  end > > In the test cases, instead of having to spell out > MyApp::SomeSpace::FooClass everywhere it is needed, it would be nice > to include MyApp::SomeSpace, and then just reference FooClass in the > tests. > > >> > Instead of making an instance of TestCase for each case, at this point >> > it looks like I'll have to create a new subclass of it. > >> Sorry, you lost me here. > > Instead of the code you presented I'd have to do something like: > >  class TestCase >    alias :_new, :new >    def self.new(&block) >      Class.new(self, &block)._new >    end >  end Then your block needs to contain a class definition. You can use the block only for _one_ thing - either class / module def or code you want to execute. Granted, you can have code executed in a class definition as well but the definition becomes only usable *after* the definition has been executed in its entirety. Maybe you haven't decided yet what you want the block for and that is causing your headaches. > Haven't tested it yet, but that should allow #include to work no > problem. Unfortunately it means defining all my dsl methods at the > class level --not even sure the instance level would be of any use in > this case either, in which case the ._new can be dropped -- kind of > stupid, just to get #include to work. But what else can I do? > > I almost feel like I'm having a mental block and there is actually an > easy way to do this. What stops you from doing the include outside? include Your::Module::Of::Choice TestCase.new Foo do x = ChoiceClass.new x.method_invocation(123) end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/