Method: ColorfulInspect.method_info

Defined in:
lib/colorful_inspect.rb

.method_info(method) ⇒ Object



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
91
# File 'lib/colorful_inspect.rb', line 52

def method_info(method)
  args = ''

  if method.respond_to?(:parameters) && (arg_ary = method.parameters)
    arg_ary.map!.each_with_index do |(type, name), index|
      name ||= "arg#{index + 1}"

      case type
      when :req   then "#{name}"
      when :opt   then "#{name} = ?"
      when :rest  then "*#{name}"
      when :block then "&#{name}"
      else name
      end
    end

    args = '(' + arg_ary.join(', ') + ')'
  elsif method.arity == 0
    args = "()"
  elsif method.arity > 0
    n = method.arity
    args = '(' + (1..n).map { |i| "arg#{i}" }.join(", ") + ')'
  elsif method.arity < 0
    n = -method.arity
    args = '(' + (1..n).map { |i| "arg#{i}" }.join(", ") + ')'
  end

  klass = if method.respond_to? :owner
            method.owner.to_s
          elsif method.inspect =~ /Method: (.*?)#/
            $1
          end

  location = if method.respond_to? :source_location
               file, line = method.source_location
               "#{file}:#{line}" if file && line
             end

  [method.name.to_s, args, klass.to_s, location]
end