I need to be able to pass the name of a variable into an expression (in cucumber) and would like to be able to convert that string into a reference (i.e. not a copy) of the variable.
e.g.
Given /^I have set an initial value to @my_var$/ do
@my_var = 10
end
# and now I want to change the value of that variable in a different step
Then /^I update "([^"]*)"$/ do |var_name_string|
# I know I can get the value of @my_var by doing:
eval "@my_var_copy = @#{var_name_string}"
# But then once I update @my_var_copy I have to finish by updating the original
eval "@#{var_name_string} = @my_var_copy"
# How do instead I create a reference to the @my_var object?
end
Since Ruby is such a reflective language I’m sure what I’m trying to do is possible but I haven’t yet cracked it.
class Reference def initialize(var_name, vars) @getter = eval "lambda { #{var_name} }", vars @setter = eval "lambda { |v| #{var_name} = v }", vars end def value @getter.call end def value=(new_value) @setter.call(new_value) end endGot this from http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc. Good luck!