Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sinatra <a href="https://github.com/sinatra/sinatra/blob/v1.3.3/lib/sinatra/base.rb#L1369-1370">aliases the <code>new</code> method to <code>new!</code></a> before redefining it, so the simplest solution is to use that instead:</p> <pre><code>def app MyApp.new! end </code></pre> <p>Of course I only noticed that <em>after</em> I’d come up with the following, which I’ll leave in as it could be useful/informative.</p> <hr> <p>A possible way to get round Sinatra <a href="https://github.com/sinatra/sinatra/blob/v1.3.3/lib/sinatra/base.rb#L1372-1377">redefining the <code>new</code> method and returning a complete Rack app</a> a get hold of an instance your actual base class is to do what the “real” <code>new</code> method does yourself:</p> <pre><code>def app a = MyApp.allocate a.send :initialize a end </code></pre> <p>This is a bit of a hack, but it might be useful for testing.</p> <p>Another technique would be to “walk” the middleware stack until you got to your class. The following is a little fragile, as it depends on all the middleware involved to use the name <code>@app</code> to refer to the next app in the stack, but this is fairly common.</p> <pre><code>def app a = MyApp.new while a.class != MyApp a = a.instance_variable_get(:@app) end a end </code></pre> <p>That won’t work on the yet to be released Sinatra 1.4 though (at least not on the current master, which is <a href="https://github.com/sinatra/sinatra/tree/41840746e866e8e8e9a0eaafc53d8b9fe6615b12">commit 41840746e866e8e8e9a0eaafc53d8b9fe6615b12</a>), as <code>new</code> now returns a <a href="https://github.com/sinatra/sinatra/blob/41840746e866e8e8e9a0eaafc53d8b9fe6615b12/lib/sinatra/base.rb#L1767-1787"><code>Wrapper</code> class</a> and the loop never ends. In this case you can grab the base class directly from the <code>@instance</code> variable:</p> <pre><code>def app MyApp.new.instance_variable_get :@instance end </code></pre> <p>(note this last technique may well change before the final 1.4 release).</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