From: "Thind, Aman" Date: 2008-08-11T13:39:20+09:00 Subject: Re: Expanding variables in a string 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 -----Original Message----- From: Frantisek ZACEK [mailto:czechripper@gmail.com] Sent: Monday, August 11, 2008 1:07 PM To: ruby-talk ML Subject: Re: Expanding variables in a string On Mon, Aug 11, 2008 at 6:00 AM, Thind, Aman wrote: > 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 > > 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. -- Frantisek ZACEK (zacek_f) -- SRS 2008 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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.