Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List methods only in a module?

Tags:

ruby

I wonder how one can list all methods in a module, but not including inherited methods.

eg.

module Software
  def exit
    puts "exited"
  end
end

puts Software.methods

Will list not only exit, but all inherited methods.

Is is possible to just list exit?

Thanks

like image 811
never_had_a_name Avatar asked Jul 29 '10 11:07

never_had_a_name


People also ask

How can I see all the methods of a module?

You can use dir(module) to see all available methods/attributes.

How do you list functions in a module?

We can list down all the functions present in a Python module by simply using the dir() method in the Python shell or in the command prompt shell.

How do I get list of methods in a Python class?

There is the dir(theobject) method to list all the fields and methods of your object (as a tuple) and the inspect module (as codeape write) to list the fields and methods with their doc (in """). Because everything (even fields) might be called in Python, I'm not sure there is a built-in function to list only methods.

How do I see all methods in Python?

Python – all() function The Python all() function returns true if all the elements of a given iterable (List, Dictionary, Tuple, set, etc.)


2 Answers

Actually Software.methods will not list exit. Software.instance_methods will list exit as well as any inherited methods (which in this case is nothing because modules don't inherit any methods unless you include another module). Software.instance_methods(false) will only list methods defined in Software.

like image 105
sepp2k Avatar answered Oct 19 '22 08:10

sepp2k


Software.public_instance_methods

seems to work for your example.

like image 33
Beanish Avatar answered Oct 19 '22 10:10

Beanish