#----------------------------------------------------- # support define and bind variable dynamically #----------------------------------------------------- $dyn_vars = nil # set value to a variable def set_var(var_name, var_value) if $dyn_vars == nil $dyn_vars = binding end strEval = var_name + "=" + var_value eval strEval, $dyn_vars end # bind string with $dyn_vars context def bind_with_dyn_vars(str) if $dyn_vars == nil $dyn_vars = binding end strEval = '"' + str + '"' return eval strEval, $dyn_vars end # evaluate operation/statement with $dyn_vars context def eval_with_dyn_vars(operation) if $dyn_vars == nil $dyn_vars = binding end eval operation, $dyn_vars end def bind_with_dyn_json_vars(json, bind_json) if json.kind_of? Hash json.each_pair do |k, v| if v.kind_of? String bind_json[bind_with_dyn_vars(k)] = bind_with_dyn_json_vars(v, bind_json) elsif v.kind_of? Hash temp = Hash.new v.each_pair do |k1, v1| temp[bind_with_dyn_vars(k1)] = bind_with_dyn_json_vars(v1, temp) end bind_json[bind_with_dyn_vars(k)] = temp elsif v.kind_of? Array temp1 = Array.new v.each {|item| temp2 = Hash.new bind_with_dyn_json_vars(item, temp2) temp1 << temp2 } bind_json[bind_with_dyn_vars(k)] = temp1 end end elsif json.kind_of? Array temp1 = Array.new json.each {|item| temp2 = Hash.new bind_with_dyn_json_vars(item, temp2) temp1 << temp2 } return temp1 else return bind_with_dyn_vars(json) end end