Rajkamal Subramanian
Rajkamal Subramanian

Reputation: 6964

Determine the class to which a method belongs in rails

 Ap::Application.routes.draw do
  resources :accounts
 end

I want to know the class or module to which the "resources" method belongs. If i search for "resources" method in http://apidock.com/rails/ (in the search text box provided), a list of classes are appearing which has the method name "resources". Confused, with knowing the origin of the method.

Is their any command which i can use in puts to see the origin.

The question is bit of beginners level.

Thanks

Upvotes: 10

Views: 3658

Answers (3)

Hoa Hoang
Hoa Hoang

Reputation: 1242

Assume that current_user is an instance of class User, you can call method function to check whether the method_name belong to class User. Example

current_user.method(:method_name).owner  
User.method(:method_name).owner 

Hope this help you!

Upvotes: 2

Jörg W Mittag
Jörg W Mittag

Reputation: 369594

Ruby is an object-oriented language. And while methods aren't objects in Ruby, you can ask Ruby to give you a Method object representing the method in question, and then you can simply tell that Method to give you its owner:

Ap::Application.routes.draw do
  p method(:resources).owner
end

Upvotes: 6

Chowlett
Chowlett

Reputation: 46675

More enlightening than searching for resources is searching for draw, since that method must do something with the block passed in.

Indeed, we find the source code for draw, which shows that the supplied block is executed in the context of a Mapper, which includes Resources, which (finally!) defines resources

Upvotes: 6

Related Questions