Skip to content

Instantly share code, notes, and snippets.

@botanicus
Created May 28, 2011 10:01
Show Gist options
  • Select an option

  • Save botanicus/996761 to your computer and use it in GitHub Desktop.

Select an option

Save botanicus/996761 to your computer and use it in GitHub Desktop.
Sinatra named regexp groups of Ruby 1.9 to name keys in params.
get %r{^/users/(?<nickname>\w+)/posts/(?<slug>[\w-]+)\.(?<extension>\w+)$} do
p params[:nickname]
end
path = "/users/botanicus/posts/hello-world.html"
path.match(%r{^/users/(\w+)/posts/([\w-]+)\.(\w+)$})
# => #<MatchData "/users/botanicus/posts/hello-world.html" 1:"botanicus" 2:"hello-world" 3:"html">
path.match(%r{^/users/(?<nickname>\w+)/posts/(?<slug>[\w-]+)\.(?<extension>\w+)$})
# => #<MatchData "/users/botanicus/posts/hello-world.html" nickname:"botanicus" slug:"hello-world" extension:"html">
# => Use params[:nickname], params[:slug] etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment