# Expanding variables in a string

**URL:** <https://www.ruby-forum.com/t/expanding-variables-in-a-string/145520>\
**Category:** Ruby\
**Created:** [August 11, 2008, 6:03am UTC](https://www.ruby-forum.com/t/expanding-variables-in-a-string/145520 "2008-08-11T06:03:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![athind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@athind](https://www.ruby-forum.com/u/athind)\
**Post date:** [August 11, 2008, 6:03am UTC](https://www.ruby-forum.com/t/expanding-variables-in-a-string/145520/1 "2008-08-11T06:03:35Z")

</div>

Hello,

If I have a string containing variables, how can I expand them?

I retrieve this string from a db so I cannot expand it at the time of  
assignment.

Eg:

myaction = ‘perl #{ENV[‘HOME\_SCRIPTS’]}/myscript.pl -#{params[:date]}’

I want to be able to do:

system(myaction)

but for this I have to expand the vars inside the script first.

Many Thanks,  
Aman

* * *

* * *

This message is intended only for the personal and confidential use of  
the designated recipient(s) named above. If you are not the intended  
recipient of this message you are hereby notified that any review,  
dissemination, distribution or copying of this message is strictly  
prohibited. This communication is for information purposes only and  
should not be regarded as an offer to sell or as a solicitation of an  
offer to buy any financial product, an official confirmation of any  
transaction, or as an official statement of Lehman Brothers. Email  
transmission cannot be guaranteed to be secure or error-free.  
Therefore, we do not represent that this information is complete or  
accurate and it should not be relied upon as such. All information is  
subject to change without notice.

---

<div class="post-metadata">

**Author:** ![athind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@athind](https://www.ruby-forum.com/u/athind)\
**Post date:** [August 11, 2008, 6:10am UTC](https://www.ruby-forum.com/t/expanding-variables-in-a-string/145520/2 "2008-08-11T06:10:01Z")

</div>

On Mon, Aug 11, 2008 at 6:00 AM, Thind, Aman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I want to be able to do:
> 
> system(myaction)
> 
> but for this I have to expand the vars inside the script first.
> 
> Many Thanks,  
> Aman

You mean you wish to execute in Ruby code you have in a string ?

If that’s wha you’re looking for then eval is what you want.

---

<div class="post-metadata">

**Author:** ![athind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@athind](https://www.ruby-forum.com/u/athind)\
**Post date:** [August 11, 2008, 6:25am UTC](https://www.ruby-forum.com/t/expanding-variables-in-a-string/145520/3 "2008-08-11T06:25:48Z")

</div>

On Aug 11, 12:00 am, “Thind, Aman” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to be able to do:
> 
> system(myaction)
> 
> but for this I have to expand the vars inside the script first.

Have you tried putting double quotes (rather than single quotes)  
around the String in question?

v = “hello”  
w = “1. #{v}” # → 1. hello  
x = ‘2. #{v}’ # → 2. #{v}

> This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice.

Given all of that, I can’t help but feel I’ve done something wrong by  
daring to answer your question… Please tell the Brothers Lehman  
that I didn’t mean to do it. Please understand that when I crossed  
the Brother Ringling, I returned home one evening to find a clown’s  
nose in my bed, so I’m a little skittish.

Eric

====

Ruby training and Rails training available at [http://LearnRuby.com](http://LearnRuby.com) .

---

<div class="post-metadata">

**Author:** ![athind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@athind](https://www.ruby-forum.com/u/athind)\
**Post date:** [August 11, 2008, 6:54am UTC](https://www.ruby-forum.com/t/expanding-variables-in-a-string/145520/4 "2008-08-11T06:54:26Z")

</div>

On Mon, Aug 11, 2008 at 6:39 AM, Thind, Aman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> NameError: undefined local variable or method `perl’ for main:Object
> 
> Thanks,  
> Aman

Sorry, I misunderstood your problem.

You could try %Q, like this  
h = {}  
h[‘MY\_THING’] = 78 # hash just for the example

myaction = %Q{perl with whaterver you want and also #{h[‘MY\_THING’]}}  
system myaction

Hope it helps.

---

<div class="post-metadata">

**Author:** ![athind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@athind](https://www.ruby-forum.com/u/athind)\
**Post date:** [August 11, 2008, 7:05am UTC](https://www.ruby-forum.com/t/expanding-variables-in-a-string/145520/5 "2008-08-11T07:05:33Z")

</div>

2008/8/11 Thind, Aman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> NameError: undefined local variable or method `perl’ for main:Object

You could try something like this:

myaction.gsub(/#{(.+?)}/){eval($1)}

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![athind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@athind](https://www.ruby-forum.com/u/athind)\
**Post date:** [August 11, 2008, 6:42am UTC](https://www.ruby-forum.com/t/expanding-variables-in-a-string/145520/6 "2008-08-11T06:42:09Z")

</div>

Thanks for the reply!

If I do eval(myaction) then it thinks all the words in the string are  
variables and complains:

irb(main):002:0\> myaction = ‘perl #{ENV[HOME\_SCRIPTS]}/myscript.pl  
-#{params[:date]}’  
=\> “perl #{ENV[HOME\_SCRIPTS]}/myscript.pl -#{params[:date]}”

irb(main):004:0\> eval myaction  
NameError: undefined local variable or method `perl’ for main:Object

What I want is for all the known variables in the string to be expanded;  
the same effect that assignment with double quotes has.

Please remember I know using double quotes in the assignment to myaction  
would solve the problem but I am doing this assignment only for  
illustration.

I actually get the string from a db query so have to expand the  
variables after the assignment.

Thanks,  
Aman

---

<div class="post-metadata">

**Author:** ![athind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@athind](https://www.ruby-forum.com/u/athind)\
**Post date:** [August 11, 2008, 11:54am UTC](https://www.ruby-forum.com/t/expanding-variables-in-a-string/145520/7 "2008-08-11T11:54:01Z")

</div>

On 11 Aug 2008, at 06:02, Heesob P. wrote:

> > irb(main):004:0\> eval myaction
> > 
> > I actually get the string from a db query so have to expand the  
> > variables after the assignment.
> 
> You could try something like this:
> 
> myaction.gsub(/#{(.+?)}/){eval($1)}

Or even  
my\_interpolated\_action = eval “%@#{action.gsub(‘@’,‘@’)}@”  
(lifted from activerecord where something similar is done)

Fred
