From: Intransition Date: 2011-11-07T07:35:13+09:00 Subject: Tricky DSL, how to do it? ------=_Part_276_31929973.1320618910818 Content-Type: multipart/alternative; boundary="----=_Part_277_28280983.1320618910823" ------=_Part_277_28280983.1320618910823 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit I'd want to write a DSL such that a surface method_missing catches undefined methods and records the blocks that they define e.g. foo do puts "foo" end So I would end up with: { :foo=># } Presently I have something like this: # class Evaluator < BasicObject def initialize(&block) @__config__ = {} instance_eval(&block) if block end def __config__ @__config__ end def method_missing(sym, *args, &block) @__config__[sym] = block end end However when I call on a block I want it to evaluate as if in the defining context (in this case toplevel), not inside the "DSL" class that evaluated via method_missing. e = Evaluator.new do foo do puts "foo" end end e.__config__[:foo].call Instead of what I want, I get a method missing error for #puts. Any ideas? ------=_Part_277_28280983.1320618910823 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable I'd want to write a DSL such that a surface method_missing catches undefine= d methods and records the blocks that they define e.g.

&= nbsp; foo do
    puts "foo"
  end
<= div>
So I would end up with:

  = { :foo=3D>#<Proc ...> }

Presently I have = something like this:

  #
&= nbsp; class Evaluator < BasicObject
    def initiali= ze(&block)
      @__config__ =3D {}
=       instance_eval(&block) if block
  &n= bsp; end

    def __config__
&n= bsp;     @__config__
    end

    def method_missing(sym, *args, &block)
      @__config__[sym] =3D block
   = ; end 
  end

However whe= n I call on a block I want it to evaluate as if in the defining context&nbs= p;(in this case toplevel), not inside the "DSL" class that evaluated via me= thod_missing. 

  e =3D Evaluator.new do<= /div>
&nb= sp;   foo do
      puts "foo"
    end
  = end

  e.__config__[:foo].call
<= br>
Instead of what I want, I get a method missing error for #put= s.

Any ideas?
------=_Part_277_28280983.1320618910823-- ------=_Part_276_31929973.1320618910818--