What I want to do is name variables dynamically like:
def instance(instance)
@instance = instance #@instance isn't actually a variable called @instance, rather a variable called @whatever was passed as an argument
end
How can I do this?
What I want to do is name variables dynamically like:
def instance(instance)
@instance = instance #@instance isn't actually a variable called @instance, rather a variable called @whatever was passed as an argument
end
How can I do this?
You can't really.
You could play around with eval, but really, it won't be readable.
Use the correct if or use a hash instead.
# With Hash:
values = {}
a = :foo
values[a] = "bar"
values[:foo] # => "bar"
# With if
calc = "bar"
if a_is_foo
foo = calc
else
oof = calc
end
foo # => "bar"
var = :a; hash = { var => 'foo' }; hash[var] = 'bar'; hash[var];eval for this. It's a design flaw and eval is dangerous in the wrong hands ;)