Module: Cond::Wrapping

Included in:
Cond
Defined in:
lib/cond/wrapping.rb

Instance Method Summary collapse

Instance Method Details

#wrap_instance_method(mod, method) ⇒ Object

Allow handlers to be called from C code by wrapping a method with begin/rescue. Returns the aliased name of the original method.

See the README.

Example:

Cond.wrap_instance_method(Fixnum, :/)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cond/wrapping.rb', line 14

def wrap_instance_method(mod, method)
  original = "cond_original_#{mod.inspect}_#{method.inspect}"
  # TODO: jettison 1.8.6, remove eval and use |&block|
  mod.module_eval "    alias_method :'\#{original}', :'\#{method}'\n    def \#{method}(*args, &block)\n      begin\n        send(:'\#{original}', *args, &block)\n      rescue Exception => e\n        raise e\n      end\n    end\n  eval_end\n  original\nend\n"

#wrap_singleton_method(mod, method) ⇒ Object

Allow handlers to be called from C code by wrapping a method with begin/rescue. Returns the aliased name of the original method.

See the README.

Example:

Cond.wrap_singleton_method(IO, :read)


40
41
42
43
# File 'lib/cond/wrapping.rb', line 40

def wrap_singleton_method(mod, method)
  singleton_class = class << mod ; self ; end
  wrap_instance_method(singleton_class, method)
end