Method: Cond.wrap_instance_method

Defined in:
lib/cond.rb

.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, :/)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cond.rb', line 180

def wrap_instance_method(mod, method)
  original = "cond_original_#{mod.inspect}_#{method.inspect}"
  # TODO: jettison 1.8.6, remove eval and use |&block|
  # TODO: fix rcov bug -- does not see %{}
  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"