10
 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

Rajkamal Subramanian
  • 6,884
  • 4
  • 52
  • 69
  • Not at all a beginner question. Rails hides a lot of its internals away - but the general reason is that _you don't usually need to know_. – Chowlett Aug 30 '11 at 15:55

3 Answers3

6

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

Chowlett
  • 45,935
  • 20
  • 116
  • 150
  • It's also worth noting that the search results for `resources` include two classes (names start with capital) and three methods; one of the methods is shown as >= v1.2.0 <= v2.3.8, so it's obsolete, and one is a member of `DeprecatedMapper`, suggesting it is too. – Chowlett Aug 30 '11 at 15:55
  • A lot of Rails is split up into modules, which is great for organizing the source but makes the documentation rather fragmented. It'd be nice if there was a better way to do this. – tadman Aug 30 '11 at 16:41
  • Thanks Chowlett. i was able to find the module 'ActionDispatch::Routing::Mapper::Resources' where the resources method is defined by following your steps. Not sure will be able to apply this method for all methods. sorry that stackoverflow allows only one answer to be marked as correct. But sure one up vote. :-) Thanks again for the help. – Rajkamal Subramanian Aug 31 '11 at 07:12
  • No worries - Jorg _did_ answer your actual question! – Chowlett Aug 31 '11 at 08:00
6

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
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
2

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!

Hoa Hoang
  • 1,212
  • 14
  • 20