FWIW, I think this bit would be easier to read to store the heredoc value into a local variable and then write it out to file in a subsequent step. What's confusing is the f.write <<-EOF nested stuff.