Class: Object

Inherits:
BasicObject
Defined in:
lib/flet.rb

Overview

Horribly disregard any OOP best practices by temporarily redefining methods for the duration of a block. Stolen from Common Lisp.

DON’T USE THIS! (unless your really mean it and there’s no other way)

Instance Method Summary collapse

Instance Method Details

#flet(bindings) ⇒ Object

:nodoc:all



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/flet.rb', line 8

def flet(bindings) # :nodoc:all
  old_methods = {}

  bindings.each do |the_method, body|
    old_methods[the_method] = method(the_method)
    define_method(the_method, body)
  end

  begin
    yield
  ensure
    bindings.each do |the_method, body|
      define_method(the_method) { |*args| old_methods[the_method].call(*args) }
    end
  end
end