class Module
METHODS_SYMBOLS = [ :inherited, :ancestors, :local, :no_acestors, :public, :private, :protected ]
# Provides an improved method lookup routnine.
# It returns a list of methods according to symbol(s) given.
#
# Usable symbols include:
#
# * :inherited or :ancestors
# * :local or :no_ancestors
# * :public
# * :private
# * :protected
# * :all
#
# If no symbol is given then :public is assumed.
# Unrecognized symbols raise an error.
#
# require 'facet/module/methods'
#
# module Demo
# def test
# puts("Hello World!")
# end
# end
#
# Demo.methods(:local) #=> ['test']
#
def methods(*args)
args = [ :public ] if args.empty?
raise ArgumentError if args.any?{ |a| ! METHODS_SYMBOLS.include?(a) }
m = []
a = args.include?( :all )
i = ( args.include?( :inherited ) or args.include?( :ancestors ) )
l = ( args.include?( :local ) or args.include?( :no_ancestors ) )
if a
m |= public_instance_methods( true )
m |= private_instance_methods( true )
m |= protected_instance_methods( true )
elsif i and l
m |= public_instance_methods( true ) if args.include?( :public )
m |= private_instance_methods( true ) if args.include?( :private )
m |= protected_instance_methods( true ) if args.include?( :protected )
elsif i
m |= (public_instance_methods( true ) - public_instance_methods( false )) if args.include?( :public )
m |= (private_instance_methods( true ) - private_instance_methods( false )) if args.include?( :private )
m |= (protected_instance_methods( true ) - protected_instance_methods( false )) if args.include?( :protected )
else
m |= public_instance_methods( ! l ) if args.include?( :public )
m |= private_instance_methods( ! l ) if args.include?( :private )
m |= protected_instance_methods( ! l ) if args.include?( :protected )
m
end
end
end
class Object
alias_method( :object_methods, :methods )
# Returns a list of methods according to symbol(s) given.
# Note that this method derives from 'facet/module/methods'.
#
# Usable symbols include:
#
# * :inherited or :ancestors
# * :local or :no_ancestors
# * :public
# * :private
# * :protected
# * :singleton
# * :all
#
# It no symbol is given then :public is assumed. Unrecognized symbols raise an error.
#
# require 'facet/module/methods'
#
# def test
# puts("Hello World!")
# end
#
# methods(:local) #=> ['test']
#
def methods(*args)
args = [ :public ] if args.empty?
m = self.class.methods( *args )
m |= singleton_methods if args.include?( :singleton ) or args.include?( :all )
m
end
end
=begin
#__TEST__
if $0 == __FILE__
p Object.methods
end
=end