Pass variables to arbitrary ruby script: ruby script.rb

M

Mario Gr

Does anyone know if there is a way to pass variables to a arbitrarily
run script? I tried this with no luck.

ruby script.rb VARIABLE=value

Thanks!
 
R

Robert Klemme

Does anyone know if there is a way to pass variables to a arbitrarily
run script? I tried this with no luck.

ruby script.rb VARIABLE=value

What variables exactly? Do you want to set shell variables? Du you
want to propagate variables from one Ruby script to another?

Kind regards

robert
 
B

Brian Candler

Mario said:
Does anyone know if there is a way to pass variables to a arbitrarily
run script? I tried this with no luck.

ruby script.rb VARIABLE=value

Option 1: parse ARGV directly. I think this is what rake does.

For example, to build a hash:

vars={}
ARGV.each do |str|
vars[$1]=$2 if str =~ /\A(.*?)=(.*)\z/
end
puts vars['VARIABLE']

Or you could use eval to set local variables if that's what you really
want (but I would strongly recommend against this)

Option 2: pass variables in the environment.

env VARIABLE=value ruby script.rb

#!/usr/bin/ruby
puts "You passed ", ENV['VARIABLE']

HTH,

Brian.
 

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,066
Latest member
VytoKetoReviews

Latest Threads

Top