Method: Module#instance_methods

Defined in:
lib/nano/module/instance_methods.rb

#instance_methods(*args) ⇒ Object

Provides an improved method lookup routnine. It returns a list of methods according to symbol(s) given.

Recognized symbols are:

  • :public include public methods

  • :private include private methods

  • :protected include protected methods

  • :ancestors include all ancestor’s methods

  • :inherited (same as above)

  • <tt>:local</tti> include non-ancestor methods

  • :all include all of the above

This method also uses the symbol-not system. So you can specify the inverse of all of the above. For instance ~:public would mean :private, :protected (see nano/symbol/not).

If no symbol is given then :public, :local is assumed. Unrecognized symbols raise an error.

module Demo
  def test
    puts("Hello World!")
  end
end

Demo.instance_methods(:local)    #=> ['test']

To maintain backward compatibility true as an intitial argument is converted to :local, :ancestors (i.e. it includes both).

Raises:

  • (ArgumentError)


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
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/nano/module/instance_methods.rb', line 40

def instance_methods(*args)

  # for backward compatibility
  args << true if args.empty?
  case args[0]
  when TrueClass
    args.shift
    args << :ancestors
    args << :local
  when FalseClass
    args.shift
    args << :local
  end

  # default public, local
  args |= [:public] unless [:publix,:private,:protected,:all].any?{ |a| args.include?(a) }
  args |= [:ancestors,:local] unless [:ancestors,:inherited,:local,:all].any?{ |a| args.include?(a) }

  raise ArgumentError if args.any?{ |a| ! METHOD_TYPES.include?(a) }

  pos, neg = args.partition { |s| ! s.not? }

  m = []

  pos.each do |n|
    case n
    when :inherited, :ancestors
      m |= ( public_instance_methods( true ) - public_instance_methods( false ) )       if pos.include?(:public)
      m |= ( private_instance_methods( true ) - private_instance_methods( false ) )     if pos.include?(:private)
      m |= ( protected_instance_methods( true ) - protected_instance_methods( false ) ) if pos.include?(:protected)
    when :local
      m |= public_instance_methods( false )    if pos.include?(:public)
      m |= private_instance_methods( false )   if pos.include?(:private)
      m |= protected_instance_methods( false ) if pos.include?(:protected)
    when :all
      m |= public_instance_methods( true )
      m |= private_instance_methods( true )
      m |= protected_instance_methods( true )
    end
  end

  neg.each do |n|
    case n
    when :public
      m -= public_instance_methods( true )
    when :private
      m -= private_instance_methods( true )
    when :protected
      m -= protected_instance_methods( true )
    when :inherited, :ancestors
      m -= ( public_instance_methods( true ) - public_instance_methods( false ) )
      m -= ( private_instance_methods( true ) - private_instance_methods( false ) )
      m -= ( protected_instance_methods( true ) - protected_instance_methods( false ) )
    when :local
      m -= public_instance_methods( false )
      m -= private_instance_methods( false )
      m -= protected_instance_methods( false )
    end
  end

  m.sort
end