Class: MethodArgs

Inherits:
Object
  • Object
show all
Defined in:
lib/method_args.rb

Overview

Code to extract the formal parameter names and types from a function given a standard Ruby function argument signature. Original code based on the MethodArgs module written by Mauricio Fernandez <[email protected]> (eigenclass.org). Updated to work with function signatures added at runtime, also, peroperly handles block arguments.

Author

Jim Powers ([email protected])

Copyright

Copyright © 2008-2009 Jim Powers

License

Distributed under the terms of the LGPLv3.0 or newer. For details see the LGPL30 file

Example:

require ‘method_args’ MethodArgsHelper.get_args(“a, b = {}, *c, &d”)

> [“a”, “b”, “*c”, “&d”]

Very useful for building proxy functions that call an underlying function

Constant Summary collapse

MAX_ARGS =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



22
23
24
# File 'lib/method_args.rb', line 22

def params
  @params
end

Instance Method Details

#output_method_info(klass, object, meth, is_singleton = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/method_args.rb', line 24

def output_method_info(klass, object, meth, is_singleton = false)
  @params = nil
  @values = nil
  num_args = 0
  unless %w[initialize].include?(meth.to_s)
    if is_singleton
      return if class << klass; private_instance_methods(true) end.include?(meth.to_s)
    else
      return if class << object; private_instance_methods(true) end.include?(meth.to_s)
    end
  end
  arity = is_singleton ? object.method(meth).arity : klass.instance_method(meth).arity
  set_trace_func lambda{|event, file, line, id, binding, classname|
    begin
      if event[/call/] && classname == MethodArgsHelper && id == meth
        @params = eval("local_variables", binding)
        @values = eval("local_variables.map{|x| eval(x)}", binding)
        throw :done
      end
    rescue Exception
    end
  }
  if arity >= 0
    num_args = arity
    catch(:done){ object.send(meth, *(0...arity)) }
  else
    # determine number of args (including splat & block)
    MAX_ARGS.downto(arity.abs - 1) do |i|
      catch(:done) do
        begin
          object.send(meth, *(0...i))
        rescue Exception
        end
      end
      next if !@values || @values.compact.empty?
      k = nil
      @values.each_with_index{|x,j| break (k = j) if Array === x}
      if k
        num_args = k+1
      else
        num_args = i
      end
    end
    args = (0...arity.abs-1).to_a
    catch(:done) do
      args.empty? ? object.send(meth) : object.send(meth, *args)
    end
  end
  set_trace_func(nil)
  fmt_params = lambda do |arr, arity|
    arr.inject([[], 0]) do |(a, i), x|
      if Array === @values[i]
        [a << "*#{x}", i+1]
      else
        [a << x, i+1]
      end
    end.first
  end
  original_params = @params
  @params ||= []
  @params = @params[0,num_args]
  @params = fmt_params.call(@params,arity)
  if @params.length < original_params.length
    @params << "&#{original_params[-1]}"
  end
  set_trace_func(nil)
end