Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Ruby 中,如何使用字符串/符号获取和设置对象的属性?
例如,如果我有一个car具有属性car.color和car.name.
car
car.color
car.name
我知道你可以做得到car.send(:color)它的属性,但我该如何设置呢?
car.send(:color)
car.send("name=", value)
或者
car.send("color=", value)
另一种方法是:
car.set_instance_variable('@color', value)