Skip to content

Instantly share code, notes, and snippets.

@IGZalbertogregoris
Created December 15, 2013 10:36
Show Gist options
  • Save IGZalbertogregoris/7971384 to your computer and use it in GitHub Desktop.
Save IGZalbertogregoris/7971384 to your computer and use it in GitHub Desktop.
In Ruby set dynamically object's attributes
class Test
attr_accessor :attribute
def initialize(attribute)
@attribute = attribute
end
end
obj = Test.new('Hello')
puts obj.attribute
attr_name = 'attribute'
obj.send("#{attr_name}=", 'Bye')
puts obj.attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment