Created
May 28, 2011 10:01
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| get %r{^/users/(?<nickname>\w+)/posts/(?<slug>[\w-]+)\.(?<extension>\w+)$} do | |
| p params[:nickname] | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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