I’m a beginning programmer, that for various reasons is using an existing Ruby codebase to learn to program. My goal is to be able to understand and eventually extend this codebase. However, I find it hard to understand what is a standard library function, a gem function, or a user-created function. Distinguishing between functions may not be the only thing that I need to do, there might be other “thingies” moving around too. The reason for the question is that I want to be able to read the code, and as I go through identify what documentation I need to read in order to understand the code.
Hence, my question is: in Ruby code, how do I distinguish between things belonging to the standard library, specific gems, and those that are user-generated, so that I may look in the right place to understand what identified functions (and other things) does?
3
Pry is extremely useful for identifying where methods come from.
[4] pry(#<ProductsController>)> show-doc respond_to
From: /usr/local/var/rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/actionpack-3.2.13/lib/action_controller/metal/mi
Owner: ActionController::MimeResponds
Visibility: public
Signature: respond_to(*mimes, &block)
Number of lines: 126
Without web-service support, an action which collects the data for displaying a list of people
might look something like this:
def index
@people = Person.all
end
...
It will also show you the source code for a method (and even let you edit it!):
[5] pry(#<ProductsController>)> show-method respond_to
From: /usr/local/var/rbenv/versions/2.0.0-p195/lib/ruby/gems/2.0.0/gems/actionpack-3.2.13/lib/action_controller/metal/mime_responds.rb @ line 191:
Owner: ActionController::MimeResponds
Visibility: public
Number of lines: 8
def respond_to(*mimes, &block)
raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
if collector = retrieve_collector_from_mimes(mimes, &block)
response = collector.response
response ? response.call : default_render({})
end
end