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

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s