I want to create dynamic variable
and assign values to them. Here is quick sample what I tried so far.
array = %w(this is a test)
array.each_with_index do |c,v|
puts "variable_#{v}".to_sym
end
which gives me output like this:
# variable_0
# variable_1
# variable_2
# variable_3
But when I am trying to assign value like below:
array.each_with_index do |c,v|
puts "variable_#{v}".to_sym = 45 # I want to assign value which will be result of api, here I just pass 45 static
end
this gives me an error:
undefined method `to_sym=' for "variable_0":String (NoMethodError)
If I removed .to_sym
it gives me an error like:
syntax error, unexpected '='
Please help me. How can I achieve this? Thanks in advance.
Note: This is just a sample code to understand how to create dynamic variables and assign them variable. In my app it's a instance_variable and I want to use them to achieve my goal.