Using a string as a variable at run time

前端 未结 6 1623
悲&欢浪女
悲&欢浪女 2020-12-05 00:20

I have a string, which has been created at runtime. I want to use this string as a variable to store some data into it. How can I convert the string into a variable name?

6条回答
  •  粉色の甜心
    2020-12-05 00:34

    You can use eval() for this provided that you've declared your variable first:

    >> foo = []
    >> eval("foo")[1] = "bar"
    >> foo[1]
    => "bar"
    

    Here are the docs.

提交回复
热议问题