| Class | Pry::Code |
| In: |
lib/pry/code/loc.rb
lib/pry/code/code_range.rb 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.
| code_type | [RW] | @return [Symbol] The type of code stored in this wrapper. |
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 [Integer, nil] start_line The line number to start on, or nil to
use the method's original line numbers.
@return [Code]
Attempt to extract the source code for module (or class) `mod`.
@param [Module, Class] mod The module (or class) of interest. @param [Integer] candidate_rank The module candidate (by rank)
to use (see `Pry::WrappedModule::Candidate` for more information).
@param [Integer, nil] start_line 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 [Integer?] start_line @param [Symbol?] code_type
Remove all lines except for the lines on either side of and including lineno.
@param [Integer] lineno @param [Integer] lines @return [Code]
Remove all lines except for the lines up to and excluding lineno.
@param [Integer] lineno @param [Integer] 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, Integer] start_line @param [Integer?] end_line @return [Code]
Get the comment that describes the expression on the given line number.
@param [Integer] line_number (1-based) @return [String] the code.
Get the multiline expression that starts on the given line number.
@param [Integer] line_number (1-based) @return [String] the code.
Remove all lines that don‘t match the given `pattern`.
@param [Regexp] pattern @return [Code]
Get the (approximate) Module.nesting at the give line number.
@param [Integer] line_number line number starting from 1 @param [Module] top_module the module in which this code exists @return [Array<Module>] a list of open modules.
Writes a formatted representation (based on the configuration of the object) to the given output, which must respond to `#<<`.
Append the given line. lineno is one more than the last existing line, unless specified otherwise.
@param [String] line @param [Integer?] lineno @return [String] The inserted line.
Take `num_lines` from `start_line`, forward or backwards.
@param [Integer] start_line @param [Integer] num_lines @return [Code]
Format output with the specified number of spaces in front of every line, unless `spaces` is falsy.
@param [Integer?] spaces @return [Code]
Format output with line numbers next to it, unless `y_n` is falsy.
@param [Boolean?] y_n @return [Code]
Format output with a marker next to the given lineno, unless lineno is falsy.
@param [Integer?] lineno @return [Code]