gpt4 book ai didi

ruby - Splat 参数与 Espresso

转载 作者:数据小太阳 更新时间:2023-10-29 08:00:50 26 4
gpt4 key购买 nike

使用 Sinatra,我可以通过以下方式将多个“未知”参数传递给路由:

get '/say/*/to/*' do
# matches /say/hello/to/world
params[:splat] # => ["hello", "world"]
end

如何在 Espresso 中做同样的事情?

最佳答案

Espresso 中的路由是常规的 Ruby 方法。

因此,如果该方法在 Ruby 中有效,则该路由将在 Espresso 中有效。

Ruby 免费提供您想要实现的目标。

只需定义一个带有预定义参数的 Ruby 方法:

require 'e'

class App < E
map '/'

def say greeting = :hello, vertor = :to, subject = :world
"say #{greeting} #{vertor} #{subject}"
end
end

# some testing
require 'sonar' # same as rack-test but a bit better
include Sonar

app App # letting Sonar know that app to test

puts get('/say').body
# => say hello to world

puts get('/say/Hi').body
# => say Hi to world

puts get('/say/Hi/from').body
# => say Hi from world

puts get('/say/Hello/from/Espresso').body
# => say Hello from Espresso

puts get('/say/Goodbye/to/Sinatra').body
# => say Goodbye to Sinatra

Working Demo

关于ruby - Splat 参数与 Espresso,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13711811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com