1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Sinatra URL Matching


Wildcards a lot like named parameters, but they match all characters up to the next maching character. A wildcard can contain one or more slash characters, so they can be used to match larger portions of the URL than a named parameter can. Any wildcard parameters will be stored in params[:splat]. If there are more than one wildcard parameters, then params[:splat] will be an array.

Since wildcards can match so much, you have to be careful when using them. A badly designed wildcard URL can block many of your other URL handlers.

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'

get '/getfile/*/:format' do
  "You requested #{params[:splat]} in #{params[:format]} format"

In this example, a full path to a file is captured using a wildcard. The wildcard matches on the final slash in the URL, followed by a normal named URL specifying the format. So, the wildcard can match long strings in a URL like /getfile/really/long/path/to/a/file.txt/xml.

  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Sinatra
  6. Splats

©2014 About.com. All rights reserved.