From: "David A. Black" Date: 2004-12-18T00:15:08+09:00 Subject: Re: String expand problem Hi -- On Fri, 17 Dec 2004, Wild Karl-Heinz wrote: > hi. > > I have a bit of a problem :-) > I'd like to do the following. > > DATA = [ > { :fn => 'data.csv', > :sql => %q{ insert into table (id,name) values ('#{x[1]}','#{x[2]}' ); } > }, > ] > > DATA.each { | d | > File.foreach( d[ :fn ] ) { | line | > x = line.chomp.split( /\t/ ) > print( d[ :sql ] ) > } > } > > If I us %Q I get an name error x isn't defined and > if I us %q the x aren't expanded with the found values. See Bill's answer too. Here's another way, using the %-style value interpolation in the output string: DATA = [ { :fn => 'data.csv', :sql => %q{ insert into table (id,name) values ('%s','%s'); } }, ] DATA.each { |d| File.foreach(d[:fn] ) { |line| x = line.chomp.split( /\t/ ) puts(d[:sql] %x) } } David -- David A. Black dblack@wobblini.net