Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Created July 31, 2012 15:41
Show Gist options
  • Select an option

  • Save zeroeth/3217924 to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/3217924 to your computer and use it in GitHub Desktop.
class eval is the devil
class Class
def attr_accessor_with_history attribute_name
attr_reader "#{attribute_name}"
attr_reader "#{attribute_name}_history"
define_method "#{attribute_name}=" do |new_value|
history = instance_variable_get("@#{attribute_name}_history") || [nil]
instance_variable_set "@#{attribute_name}_history", history
history << new_value
instance_variable_set "@#{attribute_name}", new_value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment