show a listing of methods unique to class

Discussion in 'Ruby' started by Jason Lillywhite, Aug 18, 2009.

  1. Often I find myself in IRB wanting to see a list of methods available to
    me for the object I have. So I can do object.methods and get the list.
    But what if I only want to see the methods unique to the class I'm
    working with and not the superclasses?

    For example, let's say I want to see what methods Integer has, but not
    Numeric and not Object. If I go to the Core Documentation, I find that
    the methods list should look something like:

    chr denominator downto from_prime_division gcd gcd2 gcdlcm
    induced_from lcm next numerator prime_division succ times
    to_i to_r upto

    But if I do this in IRB, it appears I only get unique class methods

    irb(main):068:0> (Integer.public_methods - Object.public_methods).sort
    => ["induced_from"]

    Is there a way to get a list that includes class AND instance methods
    that are unique?

    Thank you.
    --
    Posted via http://www.ruby-forum.com/.
    Jason Lillywhite, Aug 18, 2009
    #1
    1. Advertising

  2. I just realized that this might work:

    irb(main):073:0> (1.public_methods - Object.public_methods).sort
    => ["%", "&", "*", "**", "+", "+@", "-", "-@", "/", "<<", ">>", "[]",
    "^", "_orig_div", "_orig_mul", "abs", "angle", "arg", "between?",
    "ceil", "chr", "coerce", "conj", "conjugate", "denominator", "div",
    "divmod", "downto", "even?", "fdiv", "floor", "gcd", "id2name", "im",
    "imag", "image", "integer?", "lcm", "modulo", "next", "nonzero?",
    "numerator", "odd?", "ord", "polar", "prec", "prec_f", "prec_i", "pred",
    "quo", "real", "remainder", "round", "singleton_method_added", "size",
    "step", "succ", "times", "to_f", "to_i", "to_int", "to_sym", "truncate",
    "upto", "zero?", "|", "~"]

    So if you do 1.public_methods it shows you instance methods because 1 is
    an instance of the class Integer. If you do Integer, this is just the
    class so it will only look at class methods.

    Is this the best way to get the list of unique methods for Integer,
    including instance methods?

    --
    Posted via http://www.ruby-forum.com/.
    Jason Lillywhite, Aug 18, 2009
    #2
    1. Advertising

  3. Jason Lillywhite wrote:
    > Is this the best way to get the list of unique methods for Integer,
    > including instance methods?


    Pass "false" to #instance_methods:

    irb(main):002:0> Integer.instance_methods.size
    => 77
    irb(main):003:0> Integer.instance_methods(true).size
    => 77
    irb(main):004:0> Integer.instance_methods(false).size
    => 13


    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Aug 18, 2009
    #3
  4. Hi --

    On Wed, 19 Aug 2009, Jason Lillywhite wrote:

    > I just realized that this might work:
    >
    > irb(main):073:0> (1.public_methods - Object.public_methods).sort
    > => ["%", "&", "*", "**", "+", "+@", "-", "-@", "/", "<<", ">>", "[]",
    > "^", "_orig_div", "_orig_mul", "abs", "angle", "arg", "between?",
    > "ceil", "chr", "coerce", "conj", "conjugate", "denominator", "div",
    > "divmod", "downto", "even?", "fdiv", "floor", "gcd", "id2name", "im",
    > "imag", "image", "integer?", "lcm", "modulo", "next", "nonzero?",
    > "numerator", "odd?", "ord", "polar", "prec", "prec_f", "prec_i", "pred",
    > "quo", "real", "remainder", "round", "singleton_method_added", "size",
    > "step", "succ", "times", "to_f", "to_i", "to_int", "to_sym", "truncate",
    > "upto", "zero?", "|", "~"]
    >
    > So if you do 1.public_methods it shows you instance methods because 1 is
    > an instance of the class Integer. If you do Integer, this is just the
    > class so it will only look at class methods.
    >
    > Is this the best way to get the list of unique methods for Integer,
    > including instance methods?


    You can use the 'false' flag:

    Integer.instance_methods(false)
    Integer.methods(false)

    That will give only the methods actually defined in Integer (instance
    and class methods, respectively).


    David

    --
    David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
    Q: What's the best way to get a really solid knowledge of Ruby?
    A: Come to our Ruby training in Edison, New Jersey, September 14-17!
    Instructors: David A. Black and Erik Kastner
    More info and registration: http://rubyurl.com/vmzN
    David A. Black, Aug 18, 2009
    #4
  5. > Integer.instance_methods(false)
    > Integer.methods(false)


    Thanks folks. This is very helpful.


    --
    Posted via http://www.ruby-forum.com/.
    Jason Lillywhite, Aug 18, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KevD711
    Replies:
    1
    Views:
    362
    bruce barker
    Feb 11, 2008
  2. ToshiBoy
    Replies:
    6
    Views:
    829
    ToshiBoy
    Aug 12, 2008
  3. Oltmans
    Replies:
    6
    Views:
    324
    Terry Reedy
    Mar 11, 2009
  4. Kenneth McDonald
    Replies:
    5
    Views:
    297
    Kenneth McDonald
    Sep 26, 2008
  5. Token Type
    Replies:
    9
    Views:
    339
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page