# Exec with string in variables?

**URL:** <https://www.ruby-forum.com/t/exec-with-string-in-variables/92259>\
**Category:** Ruby\
**Created:** [April 17, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/exec-with-string-in-variables/92259 "2007-04-17T16:55:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [April 17, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/exec-with-string-in-variables/92259/1 "2007-04-17T16:55:36Z")

</div>

Hi,

i have something like this and more in perl and would transform it to  
ruby.  
First i changed system to exec and than the points to plus , but the  
strings which are in the scalar’s seems  
not be applied in the exec call , perhaps some special needs with  
quoting!?

:perl  
foreach $line(@methoden) {  
system("$exe\_path" . “dti.exe -qb -t -e$line $dom\_file  
$train\_file $modell\_string” . “$line” . “.dti”);  
…  
}

:ruby

methoden.each do |line|  
exec(“exe\_path + dti.exe -qb -t -eline dom\_file train\_file  
modell\_string + line + .dti”)  
…  
end

many thanks christian

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [April 17, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/exec-with-string-in-variables/92259/2 "2007-04-17T17:08:39Z")

</div>

Try

exec("#{exe\_path dti.exe -qb -t -eline #{dom\_file} #{train\_file}  
#{modell\_string} #{line} .dti")

Ruby needs #{variable} in quotes to parse variable. Or you could  
concatenate outside the quotes like:

exec(exe\_path + “dti.exe -qb…” + dom\_file)

Basically, it is variable + “string” + another\_variable  
On Tue, 2007-04-17 at 23:55 +0900, Christian wrote:

> :perl  
> modell\_string + line + .dti")  
> …  
> end
> 
> many thanks christian

–

Thanks,

Ken M.  
Wireless Operations Engineer  
Farmers Wireless

CONFIDENTIALITY NOTICE: This e-mail and any attachment to it may contain  
confidential and legally privileged information. This information is  
intended only for the recipient named above. If you are not the intended  
recipient, take notice that any disclosure, copying, distribution or  
taking of any action based upon this information is prohibited by law.  
If you have received this e-mail in error, please return it to the  
sender and delete this copy from your system. Thank you.

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [April 17, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/exec-with-string-in-variables/92259/3 "2007-04-17T17:21:41Z")

</div>

On Apr 17, 10:53 am, Christian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> foreach $line(@methoden) {
> 
> ```
> 
> …  
> end
> 
> many thanks christian

String interpolation does this nicely:

methoden.each do |line|  
exec(“#{exe\_path}dti.exe -qb -t -e#{line} #{dom\_file} #{train\_file}  
#{modell\_string}#{line}.dti”)  
…  
end

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [April 18, 2007, 7:33am UTC](https://www.ruby-forum.com/t/exec-with-string-in-variables/92259/4 "2007-04-18T07:33:03Z")

</div>

On Tue, Apr 17, 2007 at 11:55:06PM +0900, Christian wrote:

> :perl  
> foreach $line(@methoden) {  
> system("$exe\_path" . “dti.exe -qb -t -e$line $dom\_file  
> $train\_file $modell\_string” . “$line” . “.dti”);  
> …  
> }

Instead of $var write #{var}

Alternatively, let Ruby deal with the individual arguments:

system(exe\_path, “-qb”, “-t”, “-e” + line, dom\_file, train\_file,  
modell\_string + line + “.dti”)

This is safer as it will work even if the arguments themselves contain  
spaces.
