1

I need to do the following steps in every script in my project. Is there any way to avoid copying and paste with Ruby? parse_command_line_args and read_app_config are the methods mixed in with other module.

The following steps will appear in hundreds of scripts, I want to keep DRY:

sample_1.rb (before)

OPTIONS = parse_command_line_args
CONFIG = read_app_config(File.dirname(__FILE__), OPTIONS)
APP_NAME = CONFIG["APP_NAME"]
DB_CONFIG = YAML.load_file(File.expand_path('../config/database.yml', File.dirname(__FILE__)))
DB = DbManager.new(DB_CONFIG, DB_CONFIG["COLLECTION"][APP_NAME], APP_NAME)
~~~~

common_contants.rb

module CommonContants
 OPTIONS = parse_command_line_args
CONFIG = read_app_config(File.dirname(__FILE__), OPTIONS)
APP_NAME = CONFIG["APP_NAME"]
DB_CONFIG = YAML.load_file(File.expand_path('../config/database.yml', File.dirname(__FILE__)))
DB = DbManager.new(DB_CONFIG, DB_CONFIG["COLLECTION"][APP_NAME], APP_NAME)
end

sample_1.rb (after)

    require 'common_contants'
    include CommonContants
    ~~~~

parse_command_line_args:

8:  def parse_command_line_args
9-    options = {}
10-    OptionParser.new do |opts|
11-      opts.banner = "Usage: #{self.to_s}.rb [options]"
12-      opts.on('-f', '--config file path', 'config file') { |v| options[:app_cfg_fpath] = v }
13-    end.parse!
14-    options
15-  end

read_app_config:

17:  def read_app_config(dir_path, argv=[])
18-    if argv.has_key? :app_cfg_fpath
19-      YAML.load_file(File.expand_path(argv[:app_cfg_fpath]), dir_path)
20-    else
21-      YAML.load_file(File.expand_path('./config.yml', dir_path))
22-    end
23-  end
4
  • 3
    create module, and just include it where you want. Commented Jun 25, 2015 at 5:43
  • 1
    Why cant you put these methodes in a file and in each script you want to use them you use the require or require_relative methodes Commented Jun 25, 2015 at 8:55
  • @jonsnow module doesn't work ``<top (required)>': undefined local variable or method parse_command_line_args' for main:Object (NameError) Commented Jun 25, 2015 at 10:16
  • I think, you have to put parse_command_line_args & read_app_config method in your module. Commented Jun 25, 2015 at 10:22

1 Answer 1

1

You should turn this part of your code into a module and us it anywhere you need.

As you start to write bigger and bigger Ruby programs, you'll naturally find yourself producing chunks of reusable code---libraries of related routines that are generally applicable. You'll want to break this code out into separate files so the contents can be shared among different Ruby programs. Often this code will be organized into classes, so you'll probably stick a class (or a set of interrelated classes) into a file. However, there are times when you want to group things together that don't naturally form a class.

The answer is the module mechanism. Modules define a namespace, a sandbox in which your methods and constants can play without having to worry about being stepped on by other methods and constants.

Read more here and here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.