From: Frederick Cheung Date: 2008-08-11T18:50:35+09:00 Subject: Re: Expanding variables in a string On 11 Aug 2008, at 06:02, Heesob Park wrote: > 2008/8/11 Thind, Aman : >> 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. >> > 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