How to convert from a string to object attribute name?

前端 未结 2 1398
忘了有多久
忘了有多久 2020-12-15 06:02

I am trying to convert a string value into a name of an attribute that belongs to an object. For example, in the following code, I need all the string values in the co

2条回答
  •  有刺的猬
    2020-12-15 06:16

    Look at instance_eval method ...

    if you have 'attribute' and need do

     object.attribute = 'ololo'
    

    you can do:

    object.instance_eval('attribute') = 'ololo'
    

提交回复
热议问题