Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactoring to no block parameters in a Ruby internal DSL
    text
    copied!<p>I've got a little DSL that looks like this:</p> <pre><code>ActivityLogger.log do activity('27-06-2012') do eat do |act| act.duration = 15 act.priority = 5 end end end </code></pre> <p>I want to refactor it so it loses the block params in the innermost block, so it looks like this:</p> <pre><code>ActivityLogger.log do activity('27-06-2012') do eat do duration = 15 priority = 5 end end end </code></pre> <p>The <code>#eat</code> method instantiates a Log object: </p> <pre><code>def eat(&amp;block) @logs &lt;&lt; Log.new(Eat, &amp;block) end </code></pre> <p>Log's constructor yields <code>self</code> in the last line:</p> <pre><code>def initialize(activity, &amp;block) @activity = activity yield self end </code></pre> <p>To my mind, that is where the problem is. I've tried both using <code>instance_eval</code> in the <code>#eat</code> method (see link#2 below) and removing the <code>yield</code> statement from the Log's constructor entirely (link#3), but none of these approaches work (the Log object gets created, but doesn't get its <code>#duration</code> and <code>#priority</code> methods set).</p> <p>Here are the links:</p> <p>1) <a href="https://gist.github.com/3010757" rel="nofollow">A working DSL with block parameters</a></p> <p>2) <a href="https://gist.github.com/3010768" rel="nofollow">Non-working DSL, first refactoring attempt</a></p> <p>3) <a href="https://gist.github.com/3010784" rel="nofollow">Non-working DSL, second refactoring attempt</a></p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload