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
attr_reader attr_name+"_history"
class_eval %Q"
def #{attr_name}=(value)
if !defined? @#{attr_name}_history
@#{attr_name}_history = [@#{attr_name}]
end
@#{attr_name} = value
@#{attr_name}_history << value
end
"
end
end
class Foo
attr_accessor_with_history :bar
end
Y2xhc3MgQ2xhc3MKICAgIGRlZiBhdHRyX2FjY2Vzc29yX3dpdGhfaGlzdG9yeShhdHRyX25hbWUpCiAgICAgICAgYXR0cl9uYW1lID0gYXR0cl9uYW1lLnRvX3MgIyBtYWtlIHN1cmUgaXQncyBhIHN0cmluZwogICAgICAgIGF0dHJfcmVhZGVyIGF0dHJfbmFtZQogICAgICAgIGF0dHJfcmVhZGVyIGF0dHJfbmFtZSsiX2hpc3RvcnkiCiAgICAgICAgY2xhc3NfZXZhbCAlUSIKICAgICAgICAgICAgZGVmICN7YXR0cl9uYW1lfT0odmFsdWUpCiAgICAgICAgICAgICAgICBpZiAhZGVmaW5lZD8gQCN7YXR0cl9uYW1lfV9oaXN0b3J5CiAgICAgICAgICAgICAgICAgICAgQCN7YXR0cl9uYW1lfV9oaXN0b3J5ID0gW0Aje2F0dHJfbmFtZX1dCiAgICAgICAgICAgICAgICBlbmQKICAgICAgICAgICAgICAgIEAje2F0dHJfbmFtZX0gPSB2YWx1ZQogICAgICAgICAgICAgICAgQCN7YXR0cl9uYW1lfV9oaXN0b3J5IDw8IHZhbHVlCiAgICAgICAgICAgIGVuZAogICAgICAgICIKICAgIGVuZAplbmQKCmNsYXNzIEZvbwogICAgYXR0cl9hY2Nlc3Nvcl93aXRoX2hpc3RvcnkgOmJhcgplbmQ=