From: Paul Harrington Date: 2010-02-28T05:45:42+09:00 Subject: Re: Interpolation question Paul Harrington wrote: > Seeeeems like you're looking for eval. > > Of course, this mean you absolutely trust your input. > > Alternatively, nothing stops you from just storing the values in a hash, > doing something likke > > fields = {"wibble" => 2.0, "bobble" => 3.0} > newtext = text.gsub(%r{(.*?)}) {|value| fields[value]} > > *Still* this assumes that you absolutely know your input will always be > in this exact format, otherwise (for example if you're not the person > generating the input) use an actual XML parser (like Nokogiri). > > Dudebot wrote: >> Thanks, Luc, but that's not what I need. That produces >> >> this is an wibble >> of stuff bobble >> >> instead of >> >> this is an 2.0 >> of stuff 3.0 >> >> I need it to interpolate the value of the expressions, not the >> expressions themselves. Is this not possible in Ruby? Wow let me try that post again. $1, $2, etc are the capture globals; the value passed to the block passed to gsub is the match itself. So you want text.gsub(%r{(.*?)}) {fields[$1]} -- Posted via http://www.ruby-forum.com/.