Search code examples
rubydebuggingprybyebug

How to increase the number of lines around binding.pry?


Using the gem pry-byebug, when I add a binding.pry somewhere, it shows only 5 lines around the line that I am:

    157:       max_bytes: limits_config.max_read_bytes_per_parser,
    158:       max_reads: limits_config.max_reads_per_parser,
    159:       max_seeks: limits_config.max_seeks_per_parser
    160:     )
    161:
 => 162:     results = parsers.lazy.map do |parser|
    163:       # Reset all the read limits, per parser
    164:       limited_io.reset_limits!
    165:       read_limiter_under_cache.reset_limits!
    166:
    167:       # We need to rewind for each parser, anew

Is there a way to increase this number to show more lines of code?


Solution

  • Use whereami with an integer argument specifying the number of lines to return.

    For example, given the file foo.rb:

    # foo.rb
    
    require 'pry'
    
    def foo; end
    
    def bar; end
    
    def foobar
      # do something
      binding.pry
      # do something else
    end
    
    def baz; end
    
    def foobarbaz; end
    
    foobar
    

    Run it with ruby foo.rb:

    From: /Users/foo/foo.rb:11 Object#foobar:
    
         9: def foobar
        10:   # do something
     => 11:   binding.pry
        12:   # do something else
        13: end
    
    ⇒ 
    

    And ask to see +/- 10 lines with whereami 10:

    ⇒ whereami 10
    
    From: /Users/foo/foo.rb:11 Object#foobar:
    
         1: # foo.rb
         2:
         3: require 'pry'
         4:
         5: def foo; end
         6:
         7: def bar; end
         8:
         9: def foobar
        10:   # do something
     => 11:   binding.pry
        12:   # do something else
        13: end
        14:
        15: def baz; end
        16:
        17: def foobarbaz; end
        18:
        19: foobar
    
    ⇒ 
    

    Or just the two surrounding lines with whereami 1:

    ⇒ whereami 1
    
    From: /Users/foo/foo.rb:11 Object#foobar:
    
        10:   # do something
     => 11:   binding.pry
        12:   # do something else
    
    ⇒