bruce (owner)

Revisions

gist: 43329 Download_button fork
public
Public Clone URL: git://gist.github.com/43329.git
Embed All Files: show embed
clingwrap-example.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$:.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
 
 
output.txt #
1
2
3
4
5
6
7
8
(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]