Created
October 15, 2012 02:11
-
-
Save anonymous/3890490 to your computer and use it in GitHub Desktop.
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
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 #"your code here, use %Q for multiline strings" | |
end | |
end | |
class Foo | |
attr_accessor_with_history :bar | |
end | |
f = Foo.new() | |
f.bar = 1 | |
f.bar = 2 | |
f.bar_history | |
metaprogramming.rb:6:in `class_eval': block not supplied (ArgumentError) | |
from metaprogramming.rb:6:in `attr_accessor_with_history' | |
from metaprogramming.rb:11:in `<class:Foo>' | |
from metaprogramming.rb:10:in `<main>' | |
Sample Return Output | |
[nil, 1, 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment