The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
The multi mega method list

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jan Lelis

Posts: 136
Nickname: rbjl
Registered: Aug, 2009

Jan Lelis is an IT student from Dresden/Germany
The multi mega method list Posted: Jul 30, 2010 7:24 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jan Lelis.
Original Post: The multi mega method list
Feed Title: rbJ*_*L.net
Feed URL: http://feeds.feedburner.com/rbJL
Feed Description: Hi, I am a fan of Ruby and like to explore it and the world around ;). So I started this blog, where I am publishing code snippets, tutorials for beginners as well as general thoughts about Ruby, the web or programming in general.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jan Lelis
Latest Posts From rbJ*_*L.net

Advertisement

One of my favourite ways of learning something about existing code is to load it into irb and play around with it. You are able to ask every object in irb what it can do. It is as easy as you just asking for methods or public_methods and the object will show its abilities. But often you get spammed by Object or irb methods that you rarely want to use.

There are two common ways around this. The first is to pass a false value as the second parameter. This means, you will not get any inherited methods. Sometimes, this is what you want. However, in other cases, you maybe do not know, if the methods you are searching for are inherited ones or not.

Another approach is to remove Object’s methods from the result like this: obj.public_methods - Object.public_methods
Nevertheless, the result it not always what you have been looking for…

That is the reason why I have written a little method list helper, which groups the methods by its defining class/module and takes an optional integer parameter, how many levels of inheritance (or mixins) it should display (default is 1):

Listing 1
/31/mm.rb ruby
# mm: shows an ordered method list

module Kernel
  def method_list(levels = 1)
    if self.is_a? Module
      klass, method_function = self, :public_methods
    else
      klass, method_function = self.class, :public_instance_methods

      eigen = self.singleton_methods
      if !eigen.empty?
        puts :Eigenclass # sorry for not being up to date, I just love the word
        p self.singleton_methods
      end
    end

    levels.times{ |level|
      if cur = klass.ancestors[level]
        p cur                               # put class name
        p cur.send method_function, false   # put methods of the class
      else
        break
      end
    }

    self # or whatever
  end

  alias mm method_list
end
# J-_-L

Some examples:

# # # #
"RĂ¼be".mm
# outputs
String
[:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, :insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, :succ!, :next, :next!, :upto, :index, :rindex, :replace, :clear, :chr, :getbyte, :setbyte, :to_i, :to_f, :to_s, :to_str, :inspect, :dump, :upcase, :downcase, :capitalize, :swapcase, :upcase!, :downcase!, :capitalize!, :swapcase!, :hex, :oct, :split, :lines, :bytes, :chars, :codepoints, :reverse, :reverse!, :concat, :<<, :crypt, :intern, :to_sym, :ord, :include?, :start_with?, :end_with?, :scan, :ljust, :rjust, :center, :sub, :gsub, :chop, :chomp, :strip, :lstrip, :rstrip, :sub!, :gsub!, :chop!, :chomp!, :strip!, :lstrip!, :rstrip!, :tr, :tr_s, :delete, :squeeze, :count, :tr!, :tr_s!, :delete!, :squeeze!, :each_line, :each_byte, :each_char, :each_codepoint, :sum, :slice, :slice!, :partition, :rpartition, :encoding, :force_encoding, :valid_encoding?, :ascii_only?, :unpack, :encode, :encode!, :to_r, :to_c, :taguri=, :taguri, :is_complex_yaml?, :is_binary_data?, :to_yaml]

# # # #
99.mm 8
# outputs
Fixnum
[:to_s, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^, :[], :<<, :>>, :to_f, :size, :zero?, :odd?, :even?, :succ]
Integer
[:integer?, :odd?, :even?, :upto, :downto, :times, :succ, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :taguri=, :taguri, :to_yaml]
Numeric
[:singleton_method_added, :coerce, :i, :+@, :-@, :<=>, :eql?, :quo, :fdiv, :div, :divmod, :%, :modulo, :remainder, :abs, :magnitude, :to_int, :real?, :integer?, :zero?, :nonzero?, :floor, :ceil, :round, :truncate, :step, :numerator, :denominator, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :pretty_print_cycle, :pretty_print]
Comparable
[:==, :>, :>=, :<, :<=, :between?]
Object
[:taguri=, :taguri, :to_yaml_style, :to_yaml_properties, :syck_to_yaml, :to_yaml]
PP::ObjectMixin
[:pretty_print, :pretty_print_cycle, :pretty_print_instance_variables, :pretty_print_inspect]
Kernel
[:nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :__id__, :object_id, :to_enum, :enum_for, :pretty_inspect, :methodlist, :mm]
BasicObject
[:==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__]

# # # #
Hash.mm 99
# outputs
Hash
[:[], :try_convert, :yaml_tag_subclasses?, :allocate, :new, :superclass, :to_yaml]
Enumerable
[:freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?, :private_method_defined?, :protected_method_defined?, :public_class_method, :private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method, :pretty_print_cycle, :pretty_print, :syck_yaml_as, :yaml_as, :yaml_tag_class_name, :yaml_tag_read_class]
Object
[:yaml_tag_subclasses?, :allocate, :new, :superclass, :to_yaml]
PP::ObjectMixin
[:freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?, :private_method_defined?, :protected_method_defined?, :public_class_method, :private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method, :pretty_print_cycle, :pretty_print, :syck_yaml_as, :yaml_as, :yaml_tag_class_name, :yaml_tag_read_class]
Kernel
[:sprintf, :format, :Integer, :Float, :String, :Array, :warn, :raise, :fail, :global_variables, :__method__, :__callee__, :eval, :local_variables, :iterator?, :block_given?, :catch, :throw, :loop, :caller, :trace_var, :untrace_var, :at_exit, :syscall, :open, :printf, :print, :putc, :puts, :gets, :readline, :select, :readlines, :`, :p, :test, :srand, :rand, :trap, :exec, :fork, :exit!, :system, :spawn, :sleep, :exit, :abort, :load, :require, :require_relative, :autoload, :autoload?, :proc, :lambda, :binding, :set_trace_func, :Rational, :Complex, :pp, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?, :private_method_defined?, :protected_method_defined?, :public_class_method, :private_class_method, :instance_method, :public_instance_method, :pretty_print_cycle, :pretty_print, :syck_yaml_as, :yaml_as, :yaml_tag_class_name, :yaml_tag_read_class]
BasicObject
[:allocate, :new, :superclass, :to_yaml]

CC-BY (DE)

Read: The multi mega method list

Topic: Using the Ext JS PivotGrid Previous Topic   Next Topic Topic: spatten design is dead

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use