Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's true that you can use <code>||=</code> in this way, but it's a very strange thing to set the params <em>after</em> retrieving them. It's more likely you'll be setting variables <em>from</em> the params. So instead of this:</p> <pre><code>params[:start] ||= 0 </code></pre> <p>surely you're more likely to be doing this:</p> <pre><code>start = params[:start] || 0 </code></pre> <p>and if you're going to do that then I'd suggest using <a href="http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-fetch" rel="nofollow">fetch</a></p> <pre><code>start = params.fetch :start, 0 </code></pre> <p>If you're <em>really</em> looking for default values in the parameters hash before a route, then use a <a href="http://www.sinatrarb.com/intro.html#Filters" rel="nofollow">before filter</a></p> <pre><code>before "/comments/?" do params[:start] ||= 0 end </code></pre> <hr> <h3>Update:</h3> <p>If you're taking a parameter <strong>from the route pattern</strong> then you can give it a default argument by using block parameters, because Ruby (from v1.9) can take default parameters for blocks, e.g.</p> <pre><code>get "/comments/:start/?" do |start=0| # rest of code here end </code></pre> <p>The <code>start</code> parameter will be available via the <code>start</code> local variable (given to the block) or via <code>params[:captures].first</code> (see <a href="http://www.sinatrarb.com/intro#Routes" rel="nofollow">the docs for more on routes</a>).</p> <hr> <h3>Further update:</h3> <p>When you pass a route to a verb method (e.g. <code>get</code>) the Sinatra will use that route to match incoming requests against. Requests that match fire the block given, so a simple way to make clear that you want some defaults would be:</p> <pre><code>get "/comments/?" do defaults = {start: 10, finish: 20} params = defaults.merge params # more code follows… end </code></pre> <p>If you want it to look cleaner, use a helper:</p> <pre><code>helpers do def set_defaults( defaults={} ) warn "Entering set_defaults" # stringify_keys! h = defaults.each_with_object({}) do |(k,v),h| h[k.to_s] = defaults[k] end params.merge!( h.merge params ) end end get "/comments/?" do set_defaults start: 10, finish: 20 # more code follows… end </code></pre> <p>If you need something more heavyweight, try <a href="https://github.com/mattt/sinatra-param" rel="nofollow">sinatra-param</a>.</p> <hr> <h3>Sinatra::DefaultParameters gem</h3> <p>I liked this bit of code so much <a href="https://rubygems.org/gems/sinatra-default_parameters" rel="nofollow">I've turned it into a gem</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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