JoeyC
JoeyC

Reputation: 824

using parameters with ruby require

Is it possible to require files with arguments / parameters?

For example

resource.rb has

mode = ARGV.first

if mode == "prod"
  #use prod db
else
  #use test db
end

And main.rb requires it with params with something like

require_relative './resource.rb prod'

If this is not possible or bad practice please let me know. I guess I could use version control to switch between test and prod but it seems a little retarded.

(prod and test used only to differentiate. This is a pretty lightweight project and is not commercial if it makes any difference :) )

Using Ruby 1.9.3 on Windows XP thanks.

I am surprised that there were no answers on this already. Only stuff on using gems and using ruby with parameters but not both at the same time.

Upvotes: 6

Views: 4298

Answers (5)

knut
knut

Reputation: 27855

No, you cannot pass parameters with require. But you define a parameter before you require something.

Example ressource.rb:

RESOURCE_MODE = :prod unless defined? RESOURCE_MODE
case RESOURCE_MODE
  when :prod
    #your productive settings
  when :test
    #your test settings
  else
    raise ArgumentError
end

When you call it with require 'resource' you are running productive.

When you call it in test mode you can use:

RESOURCE_MODE = :test
require 'resource'

Another possibility: You define a resource_prod.rb and a resource_test.rb. Then you could select, which file you require.

Upvotes: 2

Jean-Louis Giordano
Jean-Louis Giordano

Reputation: 1967

You cannot do that via a require, but you could set an environment variable:

In main.rb:

ENV["mode"] = ARGV.first
require_relative './resource'

In ./resource.rb:

mode = ENV.fetch("mode")

if mode == "prod"
  #use prod db
else
  #use test db
end

The advantage of using environment variables for configurations is that you can have the same logic across multiple files. That's the Heroku way of doing things.

See Heroku's 12 factor app.

Upvotes: 9

megayu
megayu

Reputation: 161

require_relative only accept file's relative pathname relative to the requiring file’s path, you could do something like this:

# in main.rb
require_relative 'resource'

# in cli
ruby main.rb prod

Upvotes: 1

Sigurd
Sigurd

Reputation: 7953

First of all it's not going to work. App invocation via command lie differs from require internals, so there are no params passed to it. Require is read and evaluate file in a nutshell. In order support functionality you want you should use configuration and classes.

# resource.rb
class DB
  def initialize(db_config ={})
    #connect to db
  end
end

# main.rb
connection = DB.new(mode == "prod" ? prod_db_config : development_db_config)

Upvotes: 0

Dogbert
Dogbert

Reputation: 222148

No, you cannot pass parameters with require.

I'd suggest doing something like this:

def use_mode(mode)
  # your if statements
end

and then

require 'file'
use_mode :development

And wrap it up in a Class / Module for cleaner code :)

Upvotes: 6

Related Questions