This is primarily a Ruby question but I've tagged it with watir-webdriver because the example includes watir-webdriver code in the hopes that it will improve clarity.
I have a class that can both retrieve and update data found on a web page. That data gets stored in instance variables of the class.
The class includes a method that will either use the existing instance variable value to update a select list on the web page, or, if the instance variable is nil, it will get the select list's value and store it in the instance variable.
The method currently looks like this:
def get_or_select!(inst_var_sym, select_list)
value = instance_variable_get inst_var_sym
if value==nil
instance_variable_set inst_var_sym, select_list.selected_options[0].text
else
select_list.select value
end
end
That works, but I am wondering if there is a way to write the method such that it can be applied directly to the instance variable (and not a symbol matcher for the instance var) and take, as its single parameter, the select_list object.
In other words, what currently looks like this:
get_or_select!(:@instance_variable, select_list)
I would like to look like this:
@instance_variable.get_or_select!(select_list)
Is this even possible?