Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>&amp;block</code> is the way to define a method in Ruby that accepts a method and to explicitly assign the block to the variable referenced by the <code>&amp;</code>.</p> <p>Specifically, what that code does, it to accept a block, call the block passing a string as argument, then passing the block to the inner response.</p> <p>At first glance, I would say that the specific middleware you saw is basically injecting the string</p> <pre><code>"&lt;!-- #{@message}: #{@stop - @start} --&gt;\n" </code></pre> <p>into the HTTP response body if the request content type is <code>"text/html"</code>, which means an HTTP page.</p> <p>If the request is for an HTTP page, than the response is served and that string is injected. Otherwise, if the request is for another kind of file (for instance a binary file, a text file, a javascript, etc), then the response is served without any modification.</p> <p>The following example will help you to understand a little bit more the scope of <code>&amp;block</code></p> <pre><code>def foo(&amp;block) puts "foo" bar(&amp;block) end def bar(&amp;block) puts "bar" block.call end foo do puts "block" end # =&gt; foo # =&gt; bar # =&gt; block </code></pre> <p>Also note that the variable holding the block can be named anything, not necessary <code>block</code></p> <pre><code>def foo(&amp;my_block) puts "foo" bar(&amp;my_block) end def bar(&amp;another_block) puts "bar" another_block.call end foo do puts "block" end # =&gt; foo # =&gt; bar # =&gt; block </code></pre>
 

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