Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Source browsing

John Mair edited this page · 44 revisions

Source browsing

Quick Menu:

Back to Main Menu

Overview

One of Pry's killer features is its ability to display source code for methods. Pry does this without relying on any external utilities. Source browsing is an invaluable tool when you are coming to grips with a new code base and it comes into its own when the library you are exploring has little or no documentation.

View a method's source code with show-method

Simply typing show-method method_name will pull the source for the method and display it with syntax highlighting. As a convenience, Pry looks up both instance methods and class methods using this syntax, with priority given to instance methods.

You can however force show-method to look up just class methods by passing the -m switch, and instance methods using -M.

The ri method syntax is also supported. This enables you to specify both a context and a method. In ri syntax C#m specifies the instance method m on the class C. And obj.m selects the method (or class method) m on the object obj.

Note that the context and method are resolved dynamically (with respect to the current context) and so constructions such as MyClass.new.my_method are valid.

Also note that methods defined inside Pry itself are also viewable using show-method.

The following options are supported:

  • Use the -M option to select instance methods.
  • Use the -m option to select class methods.
  • Use the -c switch to select context.
  • Use the -l option to include line numbers.
  • Use the -b option to include line numbers, but start numbering at 1 (useful when used with play).

Aliases: $

Example:

pry(main)> cd Pry
pry(Pry):1> show-method rep

From: /Users/john/ruby/projects/pry/lib/pry/pry_instance.rb @ line 191:
Number of lines: 6

def rep(target=TOPLEVEL_BINDING)
  target = Pry.binding_for(target)
  result = re(target)

  show_result(result) if should_print?
end
pry(Pry):1>

Example: with ri syntax

pry(main)> show-method Grit::Blob#data -l

From: /Users/john/.rvm/gems/ruby-1.9.2-p180/gems/grit-2.4.1/lib/grit/blob.rb @ line 42:
Number of lines: 3

42: def data
43:   @data ||= @repo.git.cat_file({:p => true}, id)
44: end
pry(main)>

Example: methods defined inside Pry

pry(main)> def up_so_floating
pry(main)*   puts "anyone lives in a pretty how town"
pry(main)* end
=> nil
pry(main)> show-method up_so_floating

From: (pry) @ line 5:
Number of lines: 3

def up_so_floating
  puts "anyone lives in a pretty how town"
end
pry(main)>

Back to top

View the source code for Pry commands

In keeping with Pry's philosophy of making all source code accessible, even command source is viewable.

Simply use show-command command_name.

Use the the -l option to include line numbers.

Example:

pry(main)> show-command gem-list

From: /Users/john/.rvm/gems/ruby-1.9.2-p180/gems/pry-0.9.2/lib/pry/default_commands/gems.rb @ line 27:
Number of lines: 20

command "gem-list", "List/search installed gems. (Optional parameter: a regexp to limit the search)" do |pattern|
  pattern = Regexp.new pattern.to_s, Regexp::IGNORECASE
  gems    = if Gem::Specification.respond_to?(:each)
              Gem::Specification.select{|spec| spec.name =~ pattern }.group_by(&:name)
            else
              Gem.source_index.gems.values.group_by(&:name).select { |gemname, specs| gemname =~ pattern }
            end

  gems.each do |gem, specs|
    specs.sort! do |a,b|
      Gem::Version.new(b.version) <=> Gem::Version.new(a.version)
    end

    versions = specs.each_with_index.map do |spec, index|
      index == 0 ? text.bright_green(spec.version.to_s) : text.green(spec.version.to_s)
    end

    output.puts "#{text.white gem} (#{versions.join ', '})"
  end
end
pry(main)>

Back to top

View source for Ruby Core (MRI C code)

When the pry-doc plugin is installed (gem install pry-doc) the C source for Ruby core methods (MRI) become available. As with pure Ruby methods, the show-method command is used to display the code.

Example:

pry(main)> show-method Object#extend

From: eval.c in Ruby Core (C Method):
Number of lines: 16

static VALUE
rb_obj_extend(int argc, VALUE *argv, VALUE obj)
{
    int i;

    if (argc == 0) {
    rb_raise(rb_eArgError, "wrong number of arguments (at least 1)");
    }
    for (i = 0; i < argc; i++)
    Check_Type(argv[i], T_MODULE);
    while (argc--) {
    rb_funcall(argv[argc], rb_intern("extend_object"), 1, obj);
    rb_funcall(argv[argc], rb_intern("extended"), 1, obj);
    }
    return obj;
}
pry(main)>

Back to top

View Rubinius core source code

Rubinius is a great platform for Rubyists as nearly everything is implemented in pure Ruby. Pry takes advantage of this and the show-method command can display Rubinius core method source almost as easily as the source for standard methods.

Example:

pry(main)> show-method Enumerable#group_by

From: /Users/john/.rvm/src/rbx-head/kernel/common/enumerable.rb @ line 493:
Number of lines: 14

def group_by
  return to_enum :group_by unless block_given?
  h = {}
  i = 0
  each do |o|
    key = yield(o)
    if h.key?(key)
      h[key] << o
    else
      h[key] = [o]
    end
  end
  h
end
pry(main)>

Back to top

Something went wrong with that request. Please try again.