From: Gavin Kistner Date: 2006-09-14T00:39:46+09:00 Subject: Re: World's simplest code generator in ruby ? From: Mischa Kroon [mailto:www@bloggingabout.net] > There is an .asp application on the internet which calls itself the > World's simplest code generator it can be found here: > http://secretgeek.net/wscg.asp Here's my hackish implementation in Ruby: require 'erb' require 'csv' data = <' ) # Output all variables that are in the middle of a line while template.gsub!( /^([^%].*)\$(\d+)/, '\\1<%=line[\\2-1]%>' ) end # Output all variables left over (in ruby code) template.gsub!( /\$(\d+)/, 'line[\\1-1]' ) # Create the ERB template template = ERB.new( template, nil, '%' ) # Clean up the data to be true csv data.gsub!( /,\s+/, ',' ) # Run the template once for each line output = "" CSV::Reader.parse( data ){ |line| output << template.result( binding ) } puts output #=> tim loves fruit, but he hates veg #=> sally loves whiskey, but she hates cigars #=> (Hey sally, i also love whiskey!!!) #=> jimmy loves lager, but he hates crisps #=> jenny loves fightin', but she hates cussin'