Answer the question
In order to leave comments, you need to log in
How can you create a variable from the value of a variable in Ruby?
There is for example a variable with the value str = 'auto'.
So can I, having this variable, create a new variable with its value, i.e., create an auto variable.
If so, how?
Answer the question
In order to leave comments, you need to log in
If you have such a need, then your code = shit. But as an answer to the question, here:
stackoverflow.com/questions/17842765/how-do-i-dyna...
In Ruby, you cannot dynamically declare a local variable.
But you can define, for example, a class instance variable.
Example:
str = 'auto'
eval "@#{str}= 'Такое то значение'"
p @auto
str = 'auto'
eval "def #{str}; 'Такое то значение' end"
p auto
str = 'eval'
eval "def #{str}; 'Такое то значение' end"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question