Bill Atkins
12/17/2004 3:10:00 PM
You're getting those errors because x isn't defined at that point.
Interpolation happens as soon as Ruby encounters the string, so when
it's parsing DATA, it doesn't know what x is supposed to refer to.
There are a few ways to make this work. For instance, you could use
%q and then put some placeholders in DATA[:sql], so the string would
become "... values ('@@X1@@'..." and then simply do DATA[:sql].gsub!
/@@X1@@/, x[1] when x actually has a value.
On Fri, 17 Dec 2004 23:57:24 +0900, Wild Karl-Heinz <kh.wild@wicom.li> 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.
>
> Some hits. Whould be great :-)
>
> regards
> Karl-Heinz
>
>
--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"