class Pry::Code

object.
arbitrary chaining of formatting methods without mutating the original
which will format the text as specified when ‘#to_s` is called. This allows
In general, the formatting methods in `Code` return a new `Code` object
or method definition or be instantiated with a `String` or an `Array`.
line numbers and formats them for terminal output. It can read from a file
`Pry::Code` is a class that encapsulates lines of source code and their

def ==(other)

Returns:
  • (Boolean) -

Parameters:
  • other (Code, Object) --
def ==(other)
  if other.is_a?(Code)
    other_lines = other.instance_variable_get(:@lines)
    @lines.each_with_index.all? { |loc, i| loc == other_lines[i] }
  else
    to_s.chomp == other.to_s.chomp
  end
end

def abs_path(filename)

Returns:
  • (String) - absolute path for the given +filename+.

Raises:
  • (MethodSource::SourceNotFoundError) - if the +filename+ is not

Parameters:
  • filename (String) --
def abs_path(filename)
  abs_path = [File.expand_path(filename, Dir.pwd),
              File.expand_path(filename, Pry::INITIAL_PWD)
             ].detect { |abs_path| File.readable?(abs_path) }
  abs_path or raise MethodSource::SourceNotFoundError,
                    "Cannot open #{filename.inspect} for reading."
end

def after(lineno, lines = 1)

Returns:
  • (Code) -

Parameters:
  • lines (Integer) --
  • lineno (Integer) --
def after(lineno, lines = 1)
  return self unless lineno
  select do |loc|
    loc.lineno > lineno && loc.lineno <= lineno + lines
  end
end

def alter(&block)

class.
An abstraction of the `dup.instance_eval` pattern used throughout this
def alter(&block)
  dup.tap { |o| o.instance_eval(&block) }
end

def around(lineno, lines = 1)

Returns:
  • (Code) -

Parameters:
  • lines (Integer) --
  • lineno (Integer) --
def around(lineno, lines = 1)
  return self unless lineno
  select do |loc|
    loc.lineno >= lineno - lines && loc.lineno <= lineno + lines
  end
end

def before(lineno, lines = 1)

Returns:
  • (Code) -

Parameters:
  • lines (Integer) --
  • lineno (Integer) --
def before(lineno, lines = 1)
  return self unless lineno
  select do |loc|
    loc.lineno >= lineno - lines && loc.lineno < lineno
  end
end

def between(start_line, end_line = nil)

Returns:
  • (Code) -

Parameters:
  • end_line (Integer?) --
  • start_line (Range, Integer) --
def between(start_line, end_line = nil)
  return self unless start_line
  code_range = CodeRange.new(start_line, end_line)
  alter do
    @lines = @lines[code_range.indices_range(@lines)] || []
  end
end

def comment_describing(line_number)

Returns:
  • (String) - the code.

Parameters:
  • line_number (Integer) -- (1-based)
def comment_describing(line_number)
  self.class.comment_describing(raw, line_number)
end

def expression_at(line_number, consume = 0)

Returns:
  • (String) - the code.

Parameters:
  • line_number (Integer) -- (1-based)
def expression_at(line_number, consume = 0)
  self.class.expression_at(raw, line_number, :consume => consume)
end

def from_file(filename, code_type = type_from_filename(filename))

Returns:
  • (Code) -

Parameters:
  • code_type (Symbol) -- The type of code the file contains.
  • filename (String) -- The name of a file, or "(pry)".
def from_file(filename, code_type = type_from_filename(filename))
  code = if filename == Pry.eval_path
           Pry.line_buffer.drop(1)
         else
           File.read(abs_path(filename))
         end
  new(code, 1, code_type)
end

def from_method(meth, start_line = nil)

Returns:
  • (Code) -

Parameters:
  • start_line (Integer, nil) -- The line number to start on, or nil to
  • meth (::Method, UnboundMethod, Proc, Pry::Method) -- The method
def from_method(meth, start_line = nil)
  meth = Pry::Method(meth)
  start_line ||= meth.source_line || 1
  new(meth.source, start_line, meth.source_type)
end

def from_module(mod, candidate_rank = 0, start_line=nil)

Returns:
  • (Code) -

Parameters:
  • start_line (Integer, nil) -- The line number to start on, or nil to
  • candidate_rank (Integer) -- The module candidate (by rank)
  • mod (Module, Class) -- The module (or class) of interest.
def from_module(mod, candidate_rank = 0, start_line=nil)
  candidate = Pry::WrappedModule(mod).candidate(candidate_rank)
  start_line ||= candidate.line
  new(candidate.source, start_line, :ruby)
end

def grep(pattern)

Returns:
  • (Code) -

Parameters:
  • pattern (Regexp) --
def grep(pattern)
  return self unless pattern
  pattern = Regexp.new(pattern)
  select do |loc|
    loc.line =~ pattern
  end
end

def initialize(lines = [], start_line = 1, code_type = :ruby)

Parameters:
  • code_type (Symbol?) --
  • start_line (Integer?) --
  • lines (Array, String, IO) --
def initialize(lines = [], start_line = 1, code_type = :ruby)
  if lines.is_a? String
    lines = lines.lines
  end
  @lines = lines.each_with_index.map { |line, lineno|
    LOC.new(line, lineno + start_line.to_i) }
  @code_type = code_type
end

def inspect

Returns:
  • (String) -
def inspect
  Object.instance_method(:to_s).bind(self).call
end

def length

Returns:
  • (Integer) -
def length
  @lines ? @lines.length : 0
end

def max_lineno_width

Returns:
  • (Integer) - the number of digits in the last line.
def max_lineno_width
  @lines.length > 0 ? @lines.last.lineno.to_s.length : 0
end

def method_missing(name, *args, &block)

Forward any missing methods to the output of `#to_s`.
def method_missing(name, *args, &block)
  to_s.send(name, *args, &block)
end

def nesting_at(line_number, top_module = Object)

Returns:
  • (Array) - a list of open modules.

Parameters:
  • top_module (Module) -- the module in which this code exists
  • line_number (Integer) -- line number starting from 1
def nesting_at(line_number, top_module = Object)
  Pry::Indent.nesting_at(raw, line_number)
end

def push(line, lineno = nil)

Returns:
  • (String) - The inserted line.

Parameters:
  • lineno (Integer?) --
  • line (String) --
def push(line, lineno = nil)
  if lineno.nil?
    lineno = @lines.last.lineno + 1
  end
  @lines.push(LOC.new(line, lineno))
  line
end

def raw

Returns:
  • (String) -
def raw
  @lines.map(&:line).join("\n") + "\n"
end

def select(&block)

Returns:
  • (Code) -

Other tags:
    Yield: -
def select(&block)
  alter do
    @lines = @lines.select(&block)
  end
end

def take_lines(start_line, num_lines)

Returns:
  • (Code) -

Parameters:
  • num_lines (Integer) --
  • start_line (Integer) --
def take_lines(start_line, num_lines)
  start_idx =
    if start_line >= 0
      @lines.index { |loc| loc.lineno >= start_line } || @lines.length
    else
      @lines.length + start_line
    end
  alter do
    @lines = @lines.slice(start_idx, num_lines)
  end
end

def to_s

Returns:
  • (String) - a formatted representation (based on the configuration of
def to_s
  @lines.map { |loc|
    loc = loc.dup
    loc.colorize(@code_type)              if Pry.color
    loc.add_line_number(max_lineno_width) if @with_line_numbers
    loc.add_marker(@marker_lineno)        if @with_marker
    loc.indent(@indentation_num)          if @with_indentation
    loc.line
  }.join("\n") + "\n"
end

def type_from_filename(filename, default = :ruby)

Returns:
  • (Symbol, nil) -

Parameters:
  • default (Symbol) -- (:ruby) the file type to assume if none could be
  • filename (String) --
def type_from_filename(filename, default = :ruby)
  _, type = Pry::Code::EXTENSIONS.find do |k, _|
    k.any? { |ext| ext == File.extname(filename) }
  end
  type || default
end

def with_indentation(spaces = 0)

Returns:
  • (Code) -

Parameters:
  • spaces (Integer?) --
def with_indentation(spaces = 0)
  alter do
    @with_indentation = !!spaces
    @indentation_num  = spaces
  end
end

def with_line_numbers(y_n = true)

Returns:
  • (Code) -

Parameters:
  • y_n (Boolean?) --
def with_line_numbers(y_n = true)
  alter do
    @with_line_numbers = y_n
  end
end

def with_marker(lineno = 1)

Returns:
  • (Code) -

Parameters:
  • lineno (Integer?) --
def with_marker(lineno = 1)
  alter do
    @with_marker   = !!lineno
    @marker_lineno = lineno
  end
end