简体   繁体   中英

Ruby get methods defined in codebase

I'm trying to get a list of the methods defined in our Rails codebase, without including anything defined in superclass or dynamically defined at runtime. I've tried instance_methods(false) , but a ton of methods are returned:

> User.instance_methods(false).length
=> 310

I'm guessing this is because Rails defines a bunch of methods at runtime. Is there any way to get a list of the methods only defined in the files in our app? Hoping there's a Ruby way and not just running a grep across all of the files. Bonus points for class methods as well...

User.instance_methods will show all the inherited methods as well, so you should run something like that

User.instance_methods - User.superclass.instance_methods

Be ware thought that it will show heaps of other methods that are generated by AR when you inherited the ActiveRecord::Base class

Use MyClass.instance_methods(false) , but make sure to pass false as an argument if you don't want it to return the methods defined in the superclasses.

Additionally, use MyClass.singleton_methods(false) for class methods.

More info:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM