Module: Multimethod::ObjectExtension::ClassMethods

Defined in:
lib/multimethod/core_extensions.rb

Overview

This module is included into Object It is the “glue” for Multmethod.

Instance Method Summary collapse

Instance Method Details

#multimethod(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

Interfaces to Multimethod::Table.instance.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/multimethod/core_extensions.rb', line 30

def multimethod(body, file = nil, line = nil)
  unless file && line
    fileline = caller(1)[0]
    if fileline && md = /^(.*):(\d+)$/.match(fileline)
      file, line = md[1], md[2].to_i
      
      newlines = 0
      body.gsub(/\n/s){|x| newlines = newlines + 1}
      line -= newlines
    end
    
    # $stderr.puts "file = #{file.inspect}, line = #{line.inspect}"
  end
  
  ::Multimethod::Table.instance.install_method(self, body, file, line)
end

#remove_multimethod(signature) ⇒ Object

Removes a Multimethod using a signature:

class A
  remove_multimethod "def foo(A x)"
end


52
53
54
# File 'lib/multimethod/core_extensions.rb', line 52

def remove_multimethod(signature)
  ::Multimethod::Table.instance.remove_method(signature)
end