1

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

2
  • If it inherits from ActiveRecord::Base, you might try User.methods.sort - ActiveRecord::Base.methods Commented Jan 28, 2014 at 3:01
  • The problem is that ActiveRecord generates a lot of methods in its subclasses based on the database attributes: (User.instance_methods(false) - ActiveRecord::Base.instance_methods(false)).size # => 310 Commented Jan 28, 2014 at 6:23

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

.instance_methods(false) excludes superclass methods: ruby-doc.org/core-2.0.0/Module.html#method-i-instance_methods
0

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.