Skip to content

Instantly share code, notes, and snippets.

@Ivoz
Created October 14, 2012 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ivoz/3887650 to your computer and use it in GitHub Desktop.
Save Ivoz/3887650 to your computer and use it in GitHub Desktop.
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s
attr_reader attr_name
attr_reader attr_name+"_history"
define_method("#{attr_name}=") do |value|
prev = instance_variable_get "@#{attr_name}_history"
prev = [nil] if prev.nil?
prev << value
instance_variable_set "@#{attr_name}_history", prev
instance_variable_set "@#{attr_name}", value
end
end
end
class Foo
attr_accessor_with_history :bar
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment