# # setting local vars via eval: # # many libraries go through hoops to create a 'locals' kind of functionality # that exposes local variable to a dsl or template, here's a technique to # setup 'true' locals prior to an intance eval or method call. # class Object def scope lambda{ binding } end end class View def initialize template @template = template end def render locals = {} context = scope locals.each do |k, v| var = k value = "ObjectSpace._id2ref #{ v.object_id }" definition = "#{ var } = #{ value }" eval definition, context end double_quoted_heredoc = ['<<__template__', @template, '__template__'].join("\n") eval double_quoted_heredoc, context end end view = View.new '<body> #{ x + y } </body>' puts view.render(:x => 40, :y => 2) #=> <body> 42 </body>