In Ruby, how can I get and set a property of an object using a string/symbol?
For example, if I have an object car
with properties car.color
and car.name
.
I know you can do car.send(:color)
to get its property, but how can I set it?
Property keys must be strings or symbols (usually strings). Values can be of any type.
i know that the '?' in ruby is something that checks the yes/no fulfillment.
There are two main differences between String and Symbol in Ruby. String is mutable and Symbol is not: Because the String is mutable, it can be change in somewhere and can lead to the result is not correct. Symbol is immutable.
car.send("name=", value)
Or
car.send("color=", value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With