Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
Created December 19, 2013 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasballinger/8042661 to your computer and use it in GitHub Desktop.
Save thomasballinger/8042661 to your computer and use it in GitHub Desktop.
Add "created_at" to all ruby objects
class Class
alias :create :new
@@objects = []
def new(*a, &b)
obj = allocate
if obj.class == String
nil
elsif obj.class == Time
nil
else
puts obj.class
obj.instance_variable_set(:@created_at, Time.new)
@@objects << obj
if @@objects.length > 10
@@objects.shift
end
end
obj.class.class_eval do
define_method("created_at") {"#{Time.new - @created_at} ago"}
end
obj.class.class_eval do
define_method("objects") {@@objects.inspect}
end
obj.send(:initialize, *a, &b)
return obj
end
end
a = Array.new
b = Hash.new
sleep 1
puts a.created_at.inspect
puts a.objects.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment