Using a string as a variable at run time

前端 未结 6 1636
悲&欢浪女
悲&欢浪女 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:25

    I don't mean to be negative, but tread carefully. Ruby gives you a lot of features for highly dynamic programming such as define_method, storing blocks as Proc objects to be called later, etc. Generally these are cleaner code and far safer. 99% of the time using eval() is a mistake.

    And absolutely never use eval() on a string that contains user submitted input.

提交回复
热议问题