ruby - Splat 参数与 Espresso

标签 ruby

使用 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/

相关文章:

ruby-on-rails - rake Assets :precompile attempting to connect to database

Ruby 在文件和标准输入之间切换

arrays - 使用 ruby​​ 以 DRY 方式提取散列中的值

ruby-on-rails - 在 Windows Server : msvcrt-ruby18. dll 上构建 Ruby 堆栈

ruby - 如何从 Ruby 中的给定数组中删除重复的对值?

ruby - Enter with no input 作为有效的 bool 值

javascript - 如何在 View 中将变量从 Controller 传递到 JavaScript?

mysql - 迁移以创建表引发 Mysql2::Error: 表不存在

ruby - 在 nokogiri 的两个元素之间抓取文本?

ruby-on-rails - url 助手在 Capybara 功能测试中不使用主机名