I am learning ruby and wondering if it is possible to create a var from a variable combined with a string.
a = "variable"
a+"_name" = []
variable_name => []
Thanks, CG
You can do this easily with an instance variable as such:
>> a = "variable"
=> "variable"
>> instance_variable_set(:"@#{a}_name", [])
=> []
>> @variable_name
=> []
If you're curious about why this is easier to do with an ivar, read up about Ruby variable binding and scoping rules.
: makes it a symbol, the #{} inside the double quoted string is just normal string interpolation.But don't do that. So much easier and better to use a hash:
variable = {}
variable['name'] = []
or
vars = {}
vars[["variable", "name"]] = []