【问题标题】:ruby rack - can't modify ARGV in middleware?ruby rack - 不能在中间件中修改 ARGV?
【发布时间】:2013-09-07 04:31:10
【问题描述】:

我似乎无法在预处理的机架中间件中附加到 ARGV。任何人都知道当中间件存在时机架如何处理 ARGV?如何在我的中间件中修改 ARGV?必须有办法做到这一点,我完全不知所措。

ARGV << "--debug"
ARGV << "--host" << "localhost"

class Pre
  def initialize(app)
    @app = app
  end

  def call(env)
    if some_env_related_logic
      ARGV << "--test"
    end
    @app.call(env)
  end

end

require 'Somecommand'

use Pre
run Somecommand

现在“--test”参数不会添加到 Somecommand 中可用的 ARGV。

更新:

看起来 Somecommand 应用在初始化方法中使用了 ARGV。这发生在第一个中间件之前。所以现在的问题是:我如何创建一个调用第二个机架应用程序的机架应用程序?因为我需要在实例化秒架应用程序之前评估 ENV。

【问题讨论】:

  • 我很好奇您为什么要尝试逐个请求更改 ARGV,向 Somecommand 应用程序可以解释的 env 哈希添加特定键不是更好吗?跨度>
  • 您是否尝试过直接设置所需的变量,而不是通过 ARGV 间接设置?这就是 Ruby——你可以打开任何类并设置你想要的任何东西。

标签: ruby rack middleware argv


【解决方案1】:

尝试以下方法:

class Pre
  def initialize(app)
    @app = app
  end

  def call(env)
    # To be safe, reset the ARGV and rebuild, add any other items if needed
    ARGV.clear
    ARGV << "--debug"
    ARGV << "--host" << "localhost"

    if some_env_related_logic
      ARGV << "--test"
    end
    Somecommand.new.call(env)
  end    
end

require 'Somecommand'

# Note the change, Somecommand is no longer mentioned here 
run Pre

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2016-05-08
    • 2015-08-13
    相关资源
    最近更新 更多