gpt4 book ai didi

ruby - 将参数传递给新的 sinatra 应用程序

转载 作者:数据小太阳 更新时间:2023-10-29 06:30:58 25 4
gpt4 key购买 nike

简单的问题:我希望能够在 config.ru 中将选项传递到我的 sinatra 应用程序中。这怎么可能?我的 config.ru 看起来像这样:

run MyApp

但我想在我的 MyApp 类中使用它来接受参数:

class MyApp < Sinatra::Base
def initialize(config)
@config = config
end
end

但是我想不出一个办法来做到这一点。想法?

最佳答案

  1. 使用 set/settings

    require 'sinatra/base'

    class MyApp < Sinatra::Base
    get '/' do
    settings.time_at_startup.to_s
    end
    end

    # Just arbitrarily picking time as it'll be static but, diff for each run.
    MyApp.set :time_at_startup, Time.now

    run MyApp
  2. 使用配置文件。参见 Sinatra::ConfigFile在 contrib 中(它也使用 set 和 settings,但从 YAML 文件加载参数)

关于ruby - 将参数传递给新的 sinatra 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657202/

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