Method: Multimethod::Table#install_method

Defined in:
lib/multimethod/table.rb

#install_method(mod, body, file = nil, line = nil) ⇒ Object

Installs a new Multimethod Method using the multimethod syntax:

class A
  multimethod q{
    def foo(x)
      ...
    end
  }
  multimethod q{
    def foo(A x)
    end
  }
end

Interface to Multimethod::Module mixin multimethod



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/multimethod/table.rb', line 47

def install_method(mod, body, file = nil, line = nil)
  file ||= __FILE__
  line ||= __LINE__
  verbose = nil
  #if body =~ /def bar\(x, y\)/
  #  verbose = 1
  #end

  # Parse the signature from the method body
  signature = Signature.new
  signature.mod = mod
  signature.verbose = verbose
  signature.file = file
  signature.line = line
  new_body = signature.scan_string(body.clone)
  
  # Get our Multimethod for this
  mm = lookup_multimethod(signature.name)
  mm.install_dispatch(mod)
  m = mm.new_method_from_signature(signature)

  # Replace the multimethod signature with a plain Ruby signature.
  new_body = m.to_ruby_def + new_body

  #if true || m.signature.restarg
  #   $stderr.puts "install_method(#{mod}) => #{m.to_ruby_signature}:\n#{new_body}"
  #end

  # Evaluate the new method body.     
  mod.module_eval(new_body, file, line)
end