------art_454_14443894.1320693184523
Content-Type: multipart/alternative; 
	boundary---art_455_25769159.1320693184523"

------art_455_25769159.1320693184523
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit



On Monday, November 7, 2011 9:14:44 AM UTC-5, Sean O'Halpin wrote:
>
> You could capture the calling self by getting it from the binding of
> the block and hide the evaluating call behind your own #eval method:
>
> class Evaluator < BasicObject
>   def initialize(&block)
>     @__config__  }
>     if block
>       @context  lock.binding.eval("self")
>       instance_eval(&block)
>     end
>   end
>
>   def __config__
>     @__config__
>   end
>
>   def method_missing(sym, *args, &block)
>     @__config__[sym]  lock
>   end
>
>   def eval(sym, *args)
>     @context.instance_exec(*args, &@__config__[sym])
>   end
>
> end
>
> e  valuator.new do
>   foo do
>     puts "foo"
>   end
>
>   bar do |txt|
>     puts "txt  {txt}"
>   end
> end
>
> e.eval(:foo)               # "foo"
> e.eval(:bar, "hello")    # "txt  ello"
>
Nicely coded. And spot on. If I didn't know that self would always be 
toplevel in my particular case this is the code I would be using. 

Actually... this code seems like it might make for a good general use DSL 
pattern. I'm going to try to generalize it further and see if makes sense 
to make it a reusable library. 

What do you think?


------art_455_25769159.1320693184523
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<br><br>On Monday, November 7, 2011 9:14:44 AM UTC-5, Sean O'Halpin wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><p>You could capture the calling self by getting it from the binding of<br>the block and hide the evaluating call behind your own #eval method:</p><p>class Evaluator &lt; BasicObject<br>&nbsp; def initialize(&amp;block)<br>&nbsp; &nbsp; @__config__ = {}<br>&nbsp; &nbsp; if block<br>&nbsp; &nbsp; &nbsp; @context = block.binding.eval("self")<br>&nbsp; &nbsp; &nbsp; instance_eval(&amp;block)<br>&nbsp; &nbsp; end<br>&nbsp; end</p><p>&nbsp; def __config__<br>&nbsp; &nbsp; @__config__<br>&nbsp; end</p><p>&nbsp; def method_missing(sym, *args, &amp;block)<br>&nbsp; &nbsp; @__config__[sym] = block<br>&nbsp; end</p><p>&nbsp; def eval(sym, *args)<br>&nbsp; &nbsp; @context.instance_exec(*args, &amp;@__config__[sym])<br>&nbsp; end</p><p>end</p><p>e = Evaluator.new do<br>&nbsp; foo do<br>&nbsp; &nbsp; puts "foo"<br>&nbsp; end</p><p>&nbsp; bar do |txt|<br>&nbsp; &nbsp; puts "txt = #{txt}"<br>&nbsp; end<br>end</p><p>e.eval(:foo) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # =&gt; "foo"<br>e.eval(:bar, "hello") &nbsp; &nbsp;# =&gt; "txt = hello"</p></blockquote><div>Nicely coded. And spot on. If I didn't know that self would always beoplevel in my particular case this is the code I would be using.&nbsp;</div><div><br></div><div>Actually... this code seems like it might make for aood general use DSL pattern. I'm going to try to generalize it further and see if makes sense to make it a reusable library.&nbsp;</div><div><br></div><div>What do you think?</div><div><br></div><blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p></blockquote>
------art_455_25769159.1320693184523--

------art_454_14443894.1320693184523--