In the previous chapter we discovered how
to match path segments with the on
method. Now we
will see how to capture the value of a segment and store it in a
hash called inbox
for later use.
Dans le chapitre précédent nous avons
découvert comment matcher des chemins de segments avec la méthode
on
. Maintenant nous allons voir comment capturer la
valeur d'un segment et la stocker dans un hash appellé
inbox
pour l'utiliser plus tard.
Let's consider this value for PATH_INFO
:
Prenons cette valeur pour PATH_INFO
:
env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/hello/world", }
Here's the code to match the segment "hello"
and
capture the segment "world"
:
Voici le code qui matche avec le segment "hello"
et
qui capture le segment "world"
:
require "syro" App = Syro.new do on "hello" do on :name do get do res.text "Hello #{inbox[:name]}" end end end end env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/hello/world", } p App.call(env)
Run the code and you will get this output:
Exécutez le code et vous allez obtenir le résultat suivant :
[200, {"Content-Length"=>"11", "Content-Type"=>"text/html"}, ["Hello world"]]
The captured value gets stored in the inbox
hash
under the key passed to on
.
La valeur capturée est stockée dans le hash inbox
sous la clé donnée à on
.
What if we try a name other than "world"?
Et si nous essayons avec un nom autre que "world" ?
env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/hello/there", } p App.call(env)
[200, {"Content-Length"=>"11", "Content-Type"=>"text/html"}, ["Hello there"]]
Change the value of PATH_INFO
to
"/hello/there/world"
and modify the calls to
on
to match each segment.
Changez la valeur de PATH_INFO
par
"/hello/there/world"
et modifiez les appels à
on
pour les faire matcher avec chaque segment.