CarpeNoctem
CarpeNoctem

Reputation: 5660

ruby rack - can't modify ARGV in middleware?

I can't seem to append to ARGV within a preprocssing piece of rack middleware. Anyone know how rack handles ARGV when middleware exists? How can I modify ARGV within my middleware? There has to be a way to do this, I'm just at a complete loss.

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

Right now that "--test" argument does NOT get added to the ARGV available within Somecommand.

UPDATE:

It appears the Somecommand app makes use of ARGV in the initialize method. That happens before the first piece of middleware. So now the question is: how do I create a rack app that calls this second rack app? Since I need to evaluate the ENV before instantiation of the seconds rack app.

Upvotes: 5

Views: 1241

Answers (1)

Deepak Kumar
Deepak Kumar

Reputation: 972

Try the following:

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

Upvotes: 1

Related Questions