Show a listing of methods unique to class

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.

I just realized that this might work:

irb(main):073:0> (1.public_methods - Object.public_methods).sort
=> ["%", “&”, “*”, “**”, “+”, “[email protected]”, “-”, “[email protected]”, “/”, “<<”, “>>”, “[]”,
“^”, “_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?

Jason L. 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

Hi –

On Wed, 19 Aug 2009, Jason L. wrote:

“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

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

Thanks folks. This is very helpful.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs