1

I used to use ERB templates everywhere using the following technique:

def create_from_template(file, template, vars)
  contents = File.read(template(template))
  struct = OpenStruct.new(vars)
  result = ERB.new(contents).result(struct.binding)
  File.open(file, 'w') { |f| f.write result }
end

And I use it the following way:

app = App.new(...)
create_from_template('new_file', 'template', { :app => app })

So in my template I could have:

<%= app.name %>

And it would replace fine in REE 1.8.7, however in 1.9.2 I get the following error:

 NameError:
   undefined local variable or method `app' for #<ERB:0x007ffe6b838468>

QUESTION: How can I make this code 1.9.2 compliant?

kolrie
  • 12,562
  • 14
  • 64
  • 98

1 Answers1

1

After some research I found that another question in StackOverflow addresses this issue and proposes a fix that works:

Problem using OpenStruct with ERB

Community
  • 1
  • 1
kolrie
  • 12,562
  • 14
  • 64
  • 98