Forum: Ruby Pass variables to arbitrary ruby script: ruby script.rb

Posted by Mario Gr (mario517)
on 2009-07-03 20:07
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!
Posted by Matt Neuburg (Guest)
on 2009-07-03 20:41
(Received via mailing list)
Mario Gr <mflores3@gmail.com> wrote:

> 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

man ruby

In particular, look at the documentation of the -s switch...

m.
Posted by Robert Klemme (Guest)
on 2009-07-03 21:30
(Received via mailing list)
On 03.07.2009 20:07, Mario Gr wrote:
> 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
Posted by Brian Candler (candlerb)
on 2009-07-03 23:13
Mario Gr wrote:
> 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.
Posted by Mario Gr (mario517)
on 2009-07-04 08:00
Thanks for all the help guys!
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.