Dynamically calling accessor methods in Ruby

I have a dynamic list of attributes that I needed to set values to an object. Ruby provides a send method to bypass the usual dot notation for invoking methods.

Example

keys = ['foo', 'bar']
keys.each { |k| obj.send("#{k}=", some_value) }

See also

StackOverflow (send)

StackOverflow

Comments

Leave a comment