Using a string as a variable at run time

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

    Now simply using instance_variable_set method, you can create a instance variable at runtime.

    instance_variable_set('@' + 'users', User.all)
    

提交回复
热议问题