From: Jano Svitok Date: 2008-02-13T18:48:45+09:00 Subject: Re: ERB: import function On Feb 13, 2008 9:10 AM, Ryan Davis wrote: > > On Feb 12, 2008, at 3:48 PM, Alvaro Bautista wrote: > > > def import(fn) > > e = "" > > File.open(fn) do |f| > > e = ERB.new(f.read) > > end > > e.result > > end > > I can't help you with your actual problem, but I can suggest how to > simplify the code: > > def import path > ERB.new(File.read(path)).result > end > This is halfway to solution: def import path ERB.new(File.read(path), nil, nil, '_x_').result() end Notice the other parameters, especially the last one. ERB uses one variable to store the output. If you don't specify any, it will use a default one - and each nested ERB will overwrite the new one. The solution is 1. use another name for each nested import 2. use another binding for each nested import (i.e. make another scope, and let ERB create another var with the same name in the new scope/binding, leaving the ability to return to the previous one) I'll leave the details for your homework ;-) http://ruby-doc.org/core/classes/ERB.html#M002827 Jano