Ruby command line scripts accessing the libs folder

I am trying to create an application that will consist mainly of ruby ​​scripts that will be run from the command line (cron, in particular). I want to have a libs folder, so I can put encapsulated, reusable classes / modules there and have access to them from any script.

I want my scripts to be placed in the bin folder.

What is the best way to give them access to the libs folder? I know that I can add the boot path through the command line argument or at the top of each command line script. In PHP, sometimes it made more sense to create a custom .ini file and point the cli to the ini file, so you got them all in one pop music.

Anything like a ruby? Based on your experience, what's the best way to go here?

+3
source share
4 answers

At the top of each bin / executable you can put this at the top

#!/usr/bin/env ruby

$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib')
require 'libfile'

[etc.]

Have you been looking for something else?

If you turn your application into a Ruby gem and install the gem in your system, you don’t even need to put this material at the beginning. In this case, the require statement is sufficient.

+3
source

If you want to use your classes / modules around the world, why not just move them to your main Ruby lib directory? for example: /usr/lib/ruby/1.8/?

For instance:

$ cat > /usr/lib/ruby/1.8/mymodule.rb
module HelloWorld
    def hello
        puts("Hello, World!");
    end
end

We have our module in the main lib directory - should be able to now request it from anywhere in the system.

$ irb
irb(main):001:0> require 'mymodule'
=> true
irb(main):002:0> include HelloWorld
=> Object
irb(main):003:0> hello
Hello, World!
=> nil
+1

,

, . , Ruby , " ", eval. script . . /usr/bin, .

$ myruby <source>

. md5 md5()

#!/usr/bin/ruby -w

require 'digest/md5';

def executeCode(file)
    handle = File.open(file,'r');
    for line in handle.readlines()
        line = line.strip();
        begin
            eval(line);
        rescue Exception => e
            print "Problem with script '" + file + "'\n";
            print e + "\n";
        end
    end
end

def checkFile(file)
    if !File.exists?(file)
        print "No such source file '" + file + "'\n";
        exit(1);
    elsif !File.readable?(file)
        print "Cannot read from source file '" + file + "'\n";
        exit(1);
    else
        executeCode(file);
    end
end

# My custom function for our "interpreter"
def md5(key=nil)
    if key.nil?
        raise "md5 requires 1 parameter, 0 given!\n";
    else
        return Digest::MD5.hexdigest(key)
    end
end

if ARGV[0].nil?
    print "No input file specified!\n"
    exit(1);
else
    checkFile(ARGV[0]);
end

myruby myruby.rb (755). ruby ​​

puts "I will now generate a md5 digest for mypass using the md5() function"
puts md5('mypass')

Save this and run it like a regular ruby ​​script, but with our new interpreter. You will notice that I do not need to include any libraries or write a function in the source code, because all this is defined in our interpreter.

This is probably not the most ideal method, but it is the only one I can think of.

Greetings

+1
source

There is a RUBYLIB environment variable that can be set to any folder in the system

0
source

Source: https://habr.com/ru/post/1729975/


All Articles