class Pry::Code::LOC - pry-0.10.1 Documentation

# class Pry::Code::LOC

Represents a line of code. A line of code is a tuple, which consists of a line and a line number. A `LOC` object’s state (namely, the line parameter) can be changed via instance methods. `Pry::Code` heavily uses this class.

@api private @example

    loc = LOC.new("def example\n  :example\nend", 1)
    puts loc.line
    def example
      :example
    end
    #=> nil

    loc.indent(3)
    loc.line #=> "   def example\n  :example\nend"

### Attributes

tuple[R]

@return [Array<String, Integer>]

### Public Class Methods

new(line, lineno) click to toggle source

@param [String] line The line of code. @param [Integer] lineno The position of the `line`.

    # File lib/pry/code/loc.rb, line 27
    def initialize(line, lineno)
      @tuple = [line.chomp, lineno.to_i]
    end

### Public Instance Methods

==(other) click to toggle source

@return [Boolean]

    # File lib/pry/code/loc.rb, line 32
    def ==(other)
      other.tuple == tuple
    end

add_line_number(max_width = 0, color = false) click to toggle source

Prepends the line number `lineno` to the `line`.

@param [Integer] max_width @return [void]

    # File lib/pry/code/loc.rb, line 62
    def add_line_number(max_width = 0, color = false)
      padded = lineno.to_s.rjust(max_width)
      colorized_lineno = color ? Pry::Helpers::BaseHelpers.colorize_code(padded) : padded
      tuple[0] = "#{ colorized_lineno }: #{ line }"
    end

add_marker(marker_lineno) click to toggle source

Prepends a marker “=>” or an empty marker to the `line`.

@param [Integer] marker_lineno If it is equal to the `lineno`, then

    prepend a hashrocket. Otherwise, an empty marker.

@return [void]

    # File lib/pry/code/loc.rb, line 73
    def add_marker(marker_lineno)
      tuple[0] =
        if lineno == marker_lineno
          " => #{ line }"
        else
          "    #{ line }"
        end
    end

colorize(code_type) click to toggle source

Paints the `line` of code.

@param [Symbol] code_type @return [void]

    # File lib/pry/code/loc.rb, line 54
    def colorize(code_type)
      tuple[0] = CodeRay.scan(line, code_type).term
    end

dup() click to toggle source

    # File lib/pry/code/loc.rb, line 36
    def dup
      self.class.new(line, lineno)
    end

indent(distance) click to toggle source

Indents the `line` with `distance` spaces.

@param [Integer] distance @return [void]

    # File lib/pry/code/loc.rb, line 86
    def indent(distance)
      tuple[0] = "#{ ' ' * distance }#{ line }"
    end

line() click to toggle source

@return [String]

    # File lib/pry/code/loc.rb, line 41
    def line
      tuple.first
    end

lineno() click to toggle source

@return [Integer]

    # File lib/pry/code/loc.rb, line 46
    def lineno
      tuple.last
    end