expanding the web site
[augment.git] / lib / flet.rb
blob9595c1884a4b37220a99a73d7a457329c93f13e7
1 def Object.flet(bindings, &block)
2   old_methods = {}
4   bindings.each do |the_method, body|
5     old_methods[the_method] = method(the_method)
6     define_method(the_method, body)
7   end
8   
9   begin
10     block.call
11   ensure
12     bindings.each do |the_method, body|
13       define_method(the_method) { |*args| old_methods[the_method].call(*args) }
14     end
15   end
16 end
18 if $0 == __FILE__
19   puts "foo" # should output "foo"
20   
21   Object.flet(:puts => lambda { |str| print "#{str.reverse}\n" }) do
22     puts "foo" # should output "oof"
23   end
25   puts "foo" # should output "foo"
26 end