Skip to content

Instantly share code, notes, and snippets.

@Papillard
Last active December 19, 2015 21:29
Show Gist options
  • Save Papillard/6020824 to your computer and use it in GitHub Desktop.
Save Papillard/6020824 to your computer and use it in GitHub Desktop.
Define method to keep track on attribute history
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{
def #{attr_name}=(value)
@#{attr_name}_history.nil? ? @#{attr_name}_history = [#{attr_name}, value] : @#{attr_name}_history << value
@#{attr_name} = value
end
}
end
end
class Resto
attr_accessor_with_history :adresse
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment