Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the public methods of a class without inherited methods?

Given any object I can call #public_methods and see all the methods it will respond to. However, I find it would sometimes be handy to get a quick list of all the public methods that are not inherited, i.e. the stuff that's really part of this class.

I found in "Easy way to list public methods for a Ruby object" that if I use:

(Foo.public_methods - Object.public_methods).sort 

I can filter out a lot of basic Ruby stuff. I'd like to be able to filter everything that was inherited all the way up the chain. If I know the parent class I can filter using it, but I'd like to come up with a generic command that could return an array of the uninherited public methods for any object.

like image 540
Andrew Avatar asked Jan 13 '12 03:01

Andrew


People also ask

Can default class have public methods?

Like regular interface methods, default methods are implicitly public; there's no need to specify the public modifier. Unlike regular interface methods, we declare them with the default keyword at the beginning of the method signature, and they provide an implementation.

Why not make all methods public?

Things aren't public by default because they might contain sensitive information, or at least information that you don't want to expose as part of the class's public interface. Making something public is a bigger decision with more risks than simply making it private, so you are forced to make that decision explicitly.

How do you make a method not inheritable?

You cannot stop a derived class from inheriting all the methods of a base class. The best you can do is to make it a nonmember requiring the base object parameter. Then you would have to downcast the object before calling but you could still call it.

Are private methods inherited in Java?

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.


1 Answers

Just pass false for the inherited argument of public_methods:

"hello".public_methods.include?(:dup) # => true "hello".public_methods(false).include?(:dup) # => false 

Not an answer to your question, but in case you didn't know, irb does autocompletion, so it's easy to get the list of public methods (especially if you know the beginning of the method you are looking for). Just hit tab; hitting it twice will list all possibilities (including inherited ones, though):

> "nice".d<tab><tab> "nice".delete      "nice".delete!    "nice".display   "nice".downcase                  "nice".downcase!   "nice".dump       "nice".dup       "nice".define_singleton_method  > "nice".<tab><tab> Display all 162 possibilities? (y or n) ... 

Using pry makes it even easier to see the methods available, broken down by level of inheritance:

[1] pry(main)> cd "nice" [2] pry("nice"):1> ls Comparable#methods: <  <=  >  >=  between? String#methods: %  *  +  <<  <=>  ==  ===  =~  []  []=  ascii_only?  bytes  bytesize  byteslice  capitalize  capitalize!  casecmp  center  chars  chomp  chomp!  chop  chop!  chr  clear  codepoints  concat  count  crypt  delete  delete!  downcase  downcase!  dump  each_byte  each_char  each_codepoint  each_line  empty?  encode  encode!  encoding  end_with?  eql?  force_encoding  getbyte  gsub  gsub!  hash  hex  include?  index  insert  inspect  intern  length  lines  ljust  lstrip  lstrip!  match  next  next!  oct  ord  partition  prepend  replace  reverse  reverse!  rindex  rjust  rpartition  rstrip  rstrip!  scan  setbyte  shellescape  shellsplit  size  slice  slice!  split  squeeze  squeeze!  start_with?  strip  strip!  sub  sub!  succ  succ!  sum  swapcase  swapcase!  to_c  to_f  to_i  to_r  to_s  to_str  to_sym  tr  tr!  tr_s  tr_s!  unpack  upcase  upcase!  upto  valid_encoding? locals: _  _dir_  _ex_  _file_  _in_  _out_  _pry_ 
like image 133
Marc-André Lafortune Avatar answered Sep 25 '22 02:09

Marc-André Lafortune