Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
You can't really see it from the code (unless you know Ruby well enough of course). Maybe some IDE with good Ruby support would help by color coding standard library functions, analyzing the whole project and showing where things are defined. Here is a discussion about good IDEs for Ruby. –  thorsten müller May 22 '13 at 14:40
    
Thanks. Given that I can't see it from the code - what would be a good process for determining where "something" is derived from? –  histelheim May 22 '13 at 15:06
1  
difficult in some cases. Of course simple inheritance you see like MyClass < OtherClass, but monkey patching can play nasty tricks on you (not too mention that there are many ways to "inject" methods into existing classes). Within your code you can simply text search the project for def method_name or def self.method_name which mostly works. The gems are more difficult, since not in your project tree and are more likely to use tricky stuff (like something has_many :accounts in Rails would add a load of functionality to your class and you never would find it in your code base). –  thorsten müller May 22 '13 at 15:15

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.