Pass arguments to 'require'

T

Toby Rodwell

[You will be able to tell from what follows that I'm not a programmer -
please bear with me!]

I wrote two scripts and, for the frequent occasions I would want to run
one then the other, a third script which was just:-

#!/usr/bin/ruby

require '/opt/ruby/script1'
require '/opt/ruby/script2'

I would now like to pass arguments to the two scripts (for simplicity
the same arguments in each case), andI would like to be able to do this
via the third script eg

require '/opt/ruby/script1 #{ARGV}'
require '/opt/ruby/script2 #{ARGV}'

but Ruby doesn't accept this. For now I'm using a bash script, like so:

#!/bin/bash

/opt/ruby/script1 $@
/opt/ruby/script2 $@

but, partly for "niceness" reasons, I would like to use Ruby instead.
Any suggestions on how to do this simply? Thanks in advance.
 
R

Robert Klemme

2010/1/26 Toby Rodwell said:
[You will be able to tell from what follows that I'm not a programmer -
please bear with me!]

I wrote two scripts and, for the frequent occasions I would want to run
one then the other, a third script which was just:-

#!/usr/bin/ruby

require '/opt/ruby/script1'
require '/opt/ruby/script2'

I would now like to pass arguments to the two scripts (for simplicity
the same arguments in each case), andI would like to be able to do this
via the third script eg

require '/opt/ruby/script1 #{ARGV}'
require '/opt/ruby/script2 #{ARGV}'

but Ruby doesn't accept this. =A0For now I'm using a bash script, like so= :

#!/bin/bash

/opt/ruby/script1 $@
/opt/ruby/script2 $@

but, partly for "niceness" reasons, I would like to use Ruby instead.
Any suggestions on how to do this simply? =A0Thanks in advance.

Use system instead of require:

system '/opt/ruby/script1', *ARGV
system '/opt/ruby/script2', *ARGV

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
X

Xavier Noria

Ruby takes its arguments from a global array called ARGV. That array
is special because it is initialized by the interpreter, buy from then
on it is a regular mutable array.

In particular you can modify it:

def with_argv(*argv)
original_argv = ARGV.dup
ARGV.replace(argv)
yield
ARGV.replace(original_argv)
end

You would use that method this way:

with_argv(1, 2, 3) do
require '/opt/ruby/script1'
end

Not that it is a good practice, you normall would invoke the scripts
using #system or whatever, but since you say you are not a programmer
that would fit into your current model if you don't feel confident to
explore system.
 
R

Robert Klemme

2010/1/26 Xavier Noria said:
Ruby takes its arguments from a global array called ARGV. That array
is special because it is initialized by the interpreter, buy from then
on it is a regular mutable array.

In particular you can modify it:

=A0def with_argv(*argv)
=A0 =A0original_argv =3D ARGV.dup
=A0 =A0ARGV.replace(argv)
=A0 =A0yield
=A0 =A0ARGV.replace(original_argv)
=A0end

This is not exception safe. You'd rather want the restoration in an
ensure block. I would also return the result of yield rather than the
result of doing ARGV.replace.
You would use that method this way:

=A0with_argv(1, 2, 3) do
=A0 =A0require '/opt/ruby/script1'
=A0end

Not that it is a good practice, you normall would invoke the scripts
using #system or whatever, but since you say you are not a programmer
that would fit into your current model if you don't feel confident to
explore system.

"Require" is not a good tool in this case as it will load a script
only once and is primarily intended to be used for loading library
code. If at all I would rather use "load". So this would be an
alternative

def with_args(*args)
backup =3D ARGV.dup
begin
ARGV.replace(args)
yield
ensure
ARGV.replace(backup)
end
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
X

Xavier Noria

Sure Robert, agreed to all.

Another fine point is that in general a script will expect strings in
ARGV, so perhaps a conversion would be handy to free the caller from
this nuance.

A cool exercise anyway.
 
T

Toby Rodwell

Robert, Xavier, many thanks - not only is my problem solved but (perhaps
more importantly) my knowledge and appreciation of Ruby have grown. For
the benfit of others, here is what I now have:-

def with_argv(*args)
backup = ARGV.dup
begin
ARGV.replace(args)
yield
ensure
ARGV.replace(backup)
end
end

with_argv(*ARGV) do
require '/opt/ruby/script1.rb'
require '/opt/ruby/script2.rb'
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top