Ruby templates: How to pass variables into inlined ERB?
For a simple solution, use OpenStruct: require ‘erb’ require ‘ostruct’ namespace = OpenStruct.new(name: ‘Joan’, last: ‘Maragall’) template=”Name: <%= name %> <%= last %>” result = ERB.new(template).result(namespace.instance_eval { binding }) #=> Name: Joan Maragall The code above is simple enough but has (at least) two problems: 1) Since it relies on OpenStruct, an access to a … Read more