In the context of creating the local variable itself, it is true there are some difficulties to overcome, however assigning dynamically is still no problem.
>> my_lv = 0
=> 0
>> instance_eval("#{'my_lv'} = 42")
=> 42
>> my_lv
=> 42
So, simply create from a gathered input (from gets
, chomped or stripped as needed, it will just naturally end up as a string) and call to_sym
on it and stuff the new symbol into local_variables
and eval away...
>> local_variables << :my_created_lv
=> [:my_lv,
:__,
:_,
:_dir_,
:_file_,
:_ex_,
:_pry_,
:_out_,
:_in_,
:my_created_lv]
>>
Then you take the gathered string that you converted to a symbol, and assigned to in the code shown above, and eval it to get the value.
>> eval :my_lv.to_s
>> 24
As noted in another answer, I am unable to easily replicate this outside of Pry or IRB.
This has changed in future versions of Ruby, as Matz has removed and works hard to make this no longer able to happen.