Skip to content

Instantly share code, notes, and snippets.

@bruce
Created January 5, 2009 08:47
Show Gist options
  • Save bruce/43329 to your computer and use it in GitHub Desktop.
Save bruce/43329 to your computer and use it in GitHub Desktop.
$:.unshift(File.dirname(__FILE__) << '/../lib')
require 'clingwrap'
class Widget
def do_something
sleep 0.12
puts "Did something!"
end
end
module DoSomethingPreparation
def do_something
puts "(prepwork for Widget#do_something)"
super
end
end
Widget.clingwrap(DoSomethingPreparation)
Widget.clingwrap "Time invocation" do
def do_something
start = Time.now
result = super
puts "Elapsed: %.4fs" % (Time.now - start)
result
end
end
widget = Widget.new
widget.do_something
puts "\nUsing super-and-extend, not just an alias hack!"
puts "---\nIt also gives anonymous wrappers pretty names when inspected:"
p (class << widget; self; end).ancestors
(prepwork for Widget#do_something)
Did something!
Elapsed: 0.1201s
Using super-and-extend, not just an alias hack!
---
It also gives anonymous wrappers pretty names when inspected:
[(Wrapper:"Time invocation" @ simple.rb:21), DoSomethingPreparation, Widget, Object, Kernel]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment