2010-12-23 70 views
7

我正在玩Sinatra,我想讓我的一個路線不區分大小寫。我試圖添加這樣的路線:如何在Sinatra中創建不區分大小寫的路線?

get "(?i)/tileflood/?" do 
end 

但它不符合預期的/ tileflood任何排列。我在rubular.com上測試了以下正則表達式,它匹配得很好。我錯過了什麼嗎?

\/(?i)tileflood\/? 

回答

8

你想要一個真正的正則表達式爲你的路線:

require 'sinatra' 
get %r{^/tileflood/?$}i do 
    request.url + "\n" 
end 

證明:

smagic:~ phrogz$ curl http://localhost:4567/tileflood 
http://localhost:4567/tileflood 

smagic:~ phrogz$ curl http://localhost:4567/tIlEflOOd 
http://localhost:4567/tIlEflOOd 

smagic:~ phrogz$ curl http://localhost:4567/TILEFLOOD/ 
http://localhost:4567/TILEFLOOD/ 

smagic:~ phrogz$ curl http://localhost:4567/TILEFLOOD/z 
<!DOCTYPE html> 
<html> 
<head> 
    <style type="text/css"> 
    body { text-align:center;font-family:helvetica,arial;font-size:22px; 
    color:#888;margin:20px} 
    #c {margin:0 auto;width:500px;text-align:left} 
    </style> 
</head> 
<body> 
    <h2>Sinatra doesn't know this ditty.</h2> 
    <img src='/__sinatra__/404.png'> 
    <div id="c"> 
    Try this: 
    <pre>get '/TILEFLOOD/z' do 
    "Hello World" 
end</pre> 
    </div> 
</body> 
</html> 

smagic:~ phrogz$ curl http://localhost:4567/tileflaad 
<!DOCTYPE html> 
<html> 
<head> 
    <style type="text/css"> 
    body { text-align:center;font-family:helvetica,arial;font-size:22px; 
    color:#888;margin:20px} 
    #c {margin:0 auto;width:500px;text-align:left} 
    </style> 
</head> 
<body> 
    <h2>Sinatra doesn't know this ditty.</h2> 
    <img src='/__sinatra__/404.png'> 
    <div id="c"> 
    Try this: 
    <pre>get '/tileflaad' do 
    "Hello World" 
end</pre> 
    </div> 
</body> 
</html> 
+0

精湛。這樣做的訣竅:) – 2010-12-23 19:09:37