Created
August 7, 2013 20:28
-
-
Save vjdhama/6178303 to your computer and use it in GitHub Desktop.
Edx Class 169.1x HW 1-5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
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) | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment