From: luke gruber Date: 2011-11-13T12:14:44+09:00 Subject: Re: Advanced Ruby: Eval over multiple Bindings? There doesn't seem to be a way to 'merge' the bindings, but there are ways around it, I bet. This is kinda hacky but it works for this simple case at least: class Binding def +(other) raise "Other has to be a binding" unless Binding === other values = [] instance_variables.each do |iv| values << "#{iv[1..-1]} = #{instance_variable_get(iv)}; " end other.instance_variables.each do |iv| values << "#{iv[1..-1]} = #{other.instance_variable_get(iv)}; " end binding.tap do |bind| bind.eval values.join end end end class Test def binding1 a = 1 binding.tap do |_b| local_variables.each do |lv| _b.instance_variable_set("@#{lv}", _b.eval(lv.to_s)) unless lv =~ /_b/ end end end def binding2 b = 2 binding.tap do |bind| local_variables.each do |lv| _b.instance_variable_set("@#{lv}", _b.eval(lv.to_s)) unless lv =~ /_b/ end end end end t = Test.new puts eval('a + b', t.binding1 + t.binding2) # => 3 -- Posted via http://www.ruby-forum.com/.