Skip to content

Commit

Permalink
part5.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
jurera committed Mar 2, 2012
1 parent babef91 commit f7f7c12
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 8 deletions.
46 changes: 38 additions & 8 deletions part5.rb
Expand Up @@ -2,20 +2,50 @@ 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"
attr_reader attr_name # create the attribute's getter
attr_reader attr_name+"_history" # create bar_history getter


class_eval %Q{
def #{attr_name}_history
@#{attr_name}_history
end
def #{attr_name}_history=(value)
@#{attr_name}_history = value
end
def #{attr_name}
@#{attr_name}
end
def #{attr_name}=(value)
@#{attr_name}_history << value
@#{attr_name} = value
end
}
end

end

class Foo
def initialize()
@bar
@bar_history = Array.new
@bar_history << @bar
end

attr_accessor_with_history :bar
end

f = Foo.new
f.bar = 1
f.bar = 2
f.bar_history # => if your code works, should be [nil,1,2]
print f.bar = 1, "\n"
print f.bar = 2, "\n"
print f.bar_history, "\n" # => if your code works, should be [nil,1,2]
51 changes: 51 additions & 0 deletions part5.rb~
@@ -0,0 +1,51 @@
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}_history
@#{attr_name}_history
end

def #{attr_name}_history=(value)


@#{attr_name}_history = value
end

def #{attr_name}
@#{attr_name}
end

def #{attr_name}=(value)

@#{attr_name}_history << value
@#{attr_name} = value
end



}
end

end

class Foo
def initialize()
@bar
@bar_history = Array.new
@bar_history << @bar
end

attr_accessor_with_history :bar
end

f = Foo.new
print f.bar = 1, "\n"
print f.bar = 2, "\n"
print f.bar_history, "\n" # => if your code works, should be [nil,1,2]

0 comments on commit f7f7c12

Please sign in to comment.