fork download
  1. class Class
  2. def attr_accessor_with_history(attr_name)
  3. attr_name = attr_name.to_s # make sure it's a string
  4. attr_reader attr_name
  5. attr_reader attr_name+"_history"
  6. class_eval %Q"
  7. def #{attr_name}=(value)
  8. if !defined? @#{attr_name}_history
  9. @#{attr_name}_history = [@#{attr_name}]
  10. end
  11. @#{attr_name} = value
  12. @#{attr_name}_history << value
  13. end
  14. "
  15. end
  16. end
  17.  
  18. class Foo
  19. attr_accessor_with_history :bar
  20. end
Success #stdin #stdout 0s 4716KB
stdin
Standard input is empty
stdout
Standard output is empty