I am attempting to access an object based on a string containing the object's name. The object is scoped (I am using cancan to ensure that users can only access records they are allowed to access). I have included some code below with where I have got to:
operation_type:string, one of %w(add sub mult div)
left_operand_object:string
left_operand:string
right_operand_object:string
right_operand:string
def result
r = [access object using left_operand_object]
k = [access object using right_operand_object]
if operation_type == 'add'
r.instance_variable_get('@'+left_operand) + k.instance_variable_get('@'+left_operand)
elsif operation_type == 'sub'
r.instance_variable_get('@'+left_operand) - k.instance_variable_get('@'+left_operand)
...
end
So, two questions:
- How can I complete the
r = [access object using left_operand_object]line, to allow me to access the scoped object based on its name from the stringleft_operand_object? - Will
instance_variable_getwork ifleft_operandis something likecustomers.countor would it only work without the count method?
Any help greatly appreciated!