# Passing var to anoter script

**URL:** <https://www.ruby-forum.com/t/passing-var-to-anoter-script/115098>\
**Category:** Ruby\
**Created:** [September 20, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/passing-var-to-anoter-script/115098 "2007-09-20T16:06:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [September 20, 2007, 4:06pm UTC](https://www.ruby-forum.com/t/passing-var-to-anoter-script/115098/1 "2007-09-20T16:06:06Z")

</div>

Hey,  
I’m working on script using Watir class where I want to pass variable of  
Watir class to another script and I’m getting an error saying "syntax of  
the command is invalid’, but when I pass any other variable it works  
fine.  
Here is my code

Working ex.  
main.rb:  
$test = ‘test’  
system(“ruby “popi.rb” #{$test}”)

popi.rb  
puts $test unless $test.nil?  
puts $ie unless $ie.nil?

Not working ex.  
main.rb:  
require ‘watir’  
$ie = Watir::IE.attach(:title,/Appli.\*/)  
system(“ruby “popi.rb” #{$ie}”)

popi.rb for this is the same.

Any ideas as to why it generates syntax error?  
Thanks

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [September 20, 2007, 7:13pm UTC](https://www.ruby-forum.com/t/passing-var-to-anoter-script/115098/2 "2007-09-20T19:13:01Z")

</div>

Hi,

Am Donnerstag, 20. Sep 2007, 23:06:07 +0900 schrieb Michal S.:

> I’m working on script using Watir class where I want to pass variable of  
> Watir class to another script and I’m getting an error saying "syntax of  
> the command is invalid’, but when I pass any other variable it works  
> fine.
> 
> Not working ex.  
> main.rb:  
> require ‘watir’  
> $ie = Watir::IE.attach(:title,/Appli.\*/)  
> system(“ruby “popi.rb” #{$ie}”)

This is not a Ruby but a shell issue. Use

system “ruby”, “popi.rb”, $ie

Further, command arguments may only be strings. If you  
really need to do this anyway you could marshal the object:

## ----sub.rb class C ; end c = Marshal.load ARGV.shift puts c.inspect

## ----main.rb class C ; end c = C.new system “ruby”, “sub.rb”, Marshal.dump©

I doubt whether objects that are connected to processes  
outside could be marshalled at all.

Bertram

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [September 21, 2007, 3:08pm UTC](https://www.ruby-forum.com/t/passing-var-to-anoter-script/115098/3 "2007-09-21T15:08:11Z")

</div>

Thanks that was helpful.

Also, how can I pass values back to main script from child  
script/process?

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [September 21, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/passing-var-to-anoter-script/115098/4 "2007-09-21T15:17:20Z")

</div>

> system “ruby”, “popi.rb”, $ie
> 
> Further, command arguments may only be strings. If you  
> really need to do this anyway you could marshal the object:

What I just noticed was that it throws me an error saying that $ie  
cannot be converted to string. Why is trying to convert it to string if  
it’s the 1st arg.?  
I don’t follow that logic here…

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [September 22, 2007, 12:27am UTC](https://www.ruby-forum.com/t/passing-var-to-anoter-script/115098/5 "2007-09-22T00:27:00Z")

</div>

Hi,

Am Freitag, 21. Sep 2007, 22:17:22 +0900 schrieb Michal S.:

> > system “ruby”, “popi.rb”, $ie
> 
> What I just noticed was that it throws me an error saying that $ie  
> cannot be converted to string. Why is trying to convert it to string if  
> it’s the 1st arg.?

That was what I meant when I wrote this:

> > I doubt whether objects that are connected to processes  
> > outside could be marshalled at all.

Arguments to Unix commands are always strings; return value  
is always a 1-byte integer where 0 means success.

Bertram
