Class Pry::Code
In: lib/pry/code.rb
Parent: Object

`Pry::Code` is a class that encapsulates lines of source code and their line numbers and formats them for terminal output. It can read from a file or method definition or be instantiated with a `String` or an `Array`.

In general, the formatting methods in `Code` return a new `Code` object which will format the text as specified when `to_s` is called. This allows arbitrary chaining of formatting methods without mutating the original object.

Methods

<<   ==   after   alter   around   before   between   from_file   from_method   grep   inspect   length   method_missing   new   push   raw   select   to_s   type_from_filename   with_indentation   with_line_numbers   with_marker  

Attributes

code_type  [RW] 

Public Class methods

Instantiate a `Code` object containing code loaded from a file or Pry‘s line buffer.

@param [String] fn The name of a file, or "(pry)". @param [Symbol] code_type (:ruby) The type of code the file contains. @return [Code]

Instantiate a `Code` object containing code extracted from a `::Method`, `UnboundMethod`, `Proc`, or `Pry::Method` object.

@param [::Method, UnboundMethod, Proc, Pry::Method] meth The method

  object.

@param [Fixnum, nil] The line number to start on, or nil to use the

  method's original line numbers.

@return [Code]

Instantiate a `Code` object containing code from the given `Array`, `String`, or `IO`. The first line will be line 1 unless specified otherwise. If you need non-contiguous line numbers, you can create an empty `Code` object and then use `push` to insert the lines.

@param [Array<String>, String, IO] lines @param [Fixnum?] (1) start_line @param [Symbol?] (:ruby) code_type

Protected Class methods

Guess the CodeRay type of a file from its extension, or nil if unknown.

@param [String] filename @return [Symbol, nil]

Public Instance methods

<<(line, line_num=nil)

Alias for push

Two `Code` objects are equal if they contain the same lines with the same numbers. Otherwise, call `to_s` and `chomp` and compare as Strings.

@param [Code, Object] other @return [Boolean]

Remove all lines except for the `lines` after and excluding `line_num`.

@param [Fixnum] line_num @param [Fixnum] (1) lines @return [Code]

Remove all lines except for the `lines` on either side of and including `line_num`.

@param [Fixnum] line_num @param [Fixnum] (1) lines @return [Code]

Remove all lines except for the `lines` up to and excluding `line_num`.

@param [Fixnum] line_num @param [Fixnum] (1) lines @return [Code]

Remove all lines that aren‘t in the given range, expressed either as a `Range` object or a first and last line number (inclusive). Negative indices count from the end of the array of lines.

@param [Range, Fixnum] start_line @param [Fixnum?] end_line @return [Code]

Remove all lines that don‘t match the given `pattern`.

@param [Regexp] pattern @return [Code]

@return [String]

Return the number of lines stored.

@return [Fixnum]

Forward any missing methods to the output of `to_s`.

Append the given line. `line_num` is one more than the last existing line, unless specified otherwise.

@param [String] line @param [Fixnum?] line_num @return [String] The inserted line.

Return an unformatted String of the code.

@return [String]

Filter the lines using the given block.

@yield [line] @return [Code]

Based on the configuration of the object, return a formatted String representation.

@return [String]

Format output with the specified number of spaces in front of every line, unless `spaces` is falsy.

@param [Fixnum?] (0) spaces @return [Code]

Format output with line numbers next to it, unless `y_n` is falsy.

@param [Boolean?] (true) y_n @return [Code]

Format output with a marker next to the given `line_num`, unless `line_num` is falsy.

@param [Fixnum?] (1) line_num @return [Code]

Protected Instance methods

An abstraction of the `dup.instance_eval` pattern used throughout this class.

[Validate]