Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save zeroeth/3218000 to your computer and use it in GitHub Desktop.
playing with the devil
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s # make sure it's a string
attr_reader attr_name # create the attribute's getter
attr_reader attr_name+"_history" # create bar_history getter
class_eval %Q{
define_method "#{attr_name}=" do |value|
@#{attr_name} = value
@#{attr_name}_history ||= [nil]
@#{attr_name}_history << value
value
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment