From: Daniel Carrera Date: 2003-03-25T11:23:10+09:00 Subject: Re: exiting a loop --ZPt4rx8FFjLCG7dd Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Mar 25, 2003 at 09:32:28AM +0900, Eric Hodel wrote: > No longer a one-liner, but value-add. For bonus points, > allow specification of private, protected, singleton, etc methods. I extended it some more (file is attached). I added: - Pretty printing. - Can specify private/public/singleton methods - Can 'require' libraries without typing 'ruby -r lib methods ...' - Limited ability to examine classes that require input paramters to initialize (e.g. PStore -> store = PStore.new("file") ). USAGE: methods [options] Class BUGS: doesn't work if the parameters of Class.new are not strings. Examples: methods String methods --private --singleton String methods --require tk TkRoot methods --singleton -r tk TkRoot Available options: --require, -r Require libraries. --private Print private methods --public Print public methods --singleton Print singleton methods If none of --private, --public, --singleton is specified, public methods are printed. Example output 'methods String' (snipped): Methods for String ================== Method Arguments ~~~~~~~~~~~~~~~ ~~~~~~~~~ % 1 * 1 + 1 < 1 [snip] Example output 'methods -r pstore PStore "file"' (snipped): Methods for PStore ================== Method Arguments ~~~~~~~~~~~ ~~~~~~~~~ [] 1 []= 2 abort 0 commit 0 -- Daniel Carrera Graduate Teaching Assistant. Math Dept. University of Maryland. (301) 405-5137 --ZPt4rx8FFjLCG7dd Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=methods #!/usr/bin/env ruby -w # Print unique methods and their arity # # Can also do ruby -r methods require 'open3' require 'getoptlong' opts = GetoptLong.new( [ "--require", "-r", GetoptLong::REQUIRED_ARGUMENT ], [ "--private", GetoptLong::NO_ARGUMENT ], [ "--public", GetoptLong::NO_ARGUMENT ], [ "--singleton", GetoptLong::NO_ARGUMENT ] ) # # 'require' all necessary libraries. # keep track of any other options. # kinds_of_methods = [] opts.each do |opt, arg| if opt == "--require" require arg else kinds_of_methods.push opt end end # ======== # # Create the list of methods. # # ======== obj = eval(ARGV.shift).new(*ARGV) col_width = 0 # == size of the method with most characters. col_entries = [] # # # Find the column width. # methods = private = public = singleton = [] if kinds_of_methods == [] # Default behaviour. methods = obj.methods.sort - Object.new.methods max = methods.map {|i| i.length }.max col_width = max else if kinds_of_methods.include?("--private") private = obj.private_methods.sort - Object.new.methods max = private.map {|i| i.length }.max col_width = max end if kinds_of_methods.include?("--public") public = obj.public_methods.sort - Object.new.methods max = public.map {|i| i.length }.max col_width = [ col_width, max].max end if kinds_of_methods.include?("--singleton") singleton = obj.singleton_methods.sort - Object.new.methods max = singleton.map {|i| i.length }.max col_width = [ col_width, max].max end end # # Format the output. # if kinds_of_methods == [] # Default behaviour. methods.collect! do |meth| sprintf "%-#{col_width}s %2d",meth, obj.method(meth).arity end col_entries = methods else if kinds_of_methods.include?("--private") private.collect! do |meth| sprintf "%-#{col_width}s %2d",meth, obj.method(meth).arity end private.unshift "----------------" private.unshift "Private Methods:" private.unshift "" col_entries += private end if kinds_of_methods.include?("--public") public.collect! do |meth| sprintf "%-#{col_width}s %2d",meth, obj.method(meth).arity end public.unshift "---------------" public.unshift "Public Methods:" public.unshift "" col_entries += public end if kinds_of_methods.include?("--singleton") singleton.collect! do |meth| sprintf "%-#{col_width}s %2d",meth, obj.method(meth).arity end singleton.unshift "-----------------" singleton.unshift "Singleton Methods" singleton.unshift "" col_entries += singleton end end # # Produce the output. # #exit IO.popen('less', 'w') do |less| less.puts "Methods for #{obj.class}" less.puts "=" * (12 + obj.class.to_s.length) less.puts "" less.puts(sprintf("%-#{col_width}s Arguments", "Method")) less.puts("~"*col_width + " " + "~"*9) less.puts col_entries.join($/) end --ZPt4rx8FFjLCG7dd--