Class: Pry::Code

Inherits:
Object show all
Extended by:
MethodSource::CodeHelpers
Defined in:
lib/pry/code.rb,
lib/pry/code/loc.rb,
lib/pry/code/code_range.rb

Overview

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.

Defined Under Namespace

Classes: CodeRange, LOC

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

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.

Parameters:

  • lines (Array<String>, String, IO) (defaults to: [])
  • start_line (Integer?) (defaults to: 1)
  • code_type (Symbol?) (defaults to: :ruby)


88
89
90
91
92
93
94
95
# File 'lib/pry/code.rb', line 88

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(name, *args, &block)

Forward any missing methods to the output of #to_s.



329
330
331
# File 'lib/pry/code.rb', line 329

def method_missing(name, *args, &block)
  to_s.send(name, *args, &block)
end

Instance Attribute Details

- (Symbol) code_type

Returns The type of code stored in this wrapper.

Returns:

  • (Symbol)

    The type of code stored in this wrapper.



78
79
80
# File 'lib/pry/code.rb', line 78

def code_type
  @code_type
end

Class Method Details

+ (Code) from_file(filename, code_type = nil)

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

Parameters:

  • filename (String)

    The name of a file, or "(pry)".

  • code_type (Symbol) (defaults to: nil)

    The type of code the file contains.

Returns:



42
43
44
45
46
# File 'lib/pry/code.rb', line 42

def from_file(filename, code_type = nil)
  code_file = CodeFile.new(filename, code_type)
  # binding.pry
  new(code_file.code, 1, code_file.code_type)
end

+ (Code) from_method(meth, start_line = nil)

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

Parameters:

  • meth (::Method, UnboundMethod, Proc, Pry::Method)

    The method object.

  • start_line (Integer, nil) (defaults to: nil)

    The line number to start on, or nil to use the method's original line numbers.

Returns:



56
57
58
59
60
# File 'lib/pry/code.rb', line 56

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

+ (Code) from_module(mod, candidate_rank = 0, start_line = nil)

Attempt to extract the source code for module (or class) mod.

Parameters:

  • mod (Module, Class)

    The module (or class) of interest.

  • candidate_rank (Integer) (defaults to: 0)

    The module candidate (by rank) to use (see Pry::WrappedModule::Candidate for more information).

  • start_line (Integer, nil) (defaults to: nil)

    The line number to start on, or nil to use the method's original line numbers.

Returns:



70
71
72
73
74
# File 'lib/pry/code.rb', line 70

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

Instance Method Details

- (Boolean) ==(other)

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.

Parameters:

Returns:

  • (Boolean)


319
320
321
322
323
324
325
326
# File 'lib/pry/code.rb', line 319

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

- (Code) after(lineno, lines = 1)

Remove all lines except for the +lines+ after and excluding +lineno+.

Parameters:

  • lineno (Integer)
  • lines (Integer) (defaults to: 1)

Returns:



189
190
191
192
193
194
195
# File 'lib/pry/code.rb', line 189

def after(lineno, lines = 1)
  return self unless lineno

  select do |loc|
    loc.lineno > lineno && loc.lineno <= lineno + lines
  end
end

- (Code) around(lineno, lines = 1)

Remove all lines except for the +lines+ on either side of and including +lineno+.

Parameters:

  • lineno (Integer)
  • lines (Integer) (defaults to: 1)

Returns:



176
177
178
179
180
181
182
# File 'lib/pry/code.rb', line 176

def around(lineno, lines = 1)
  return self unless lineno

  select do |loc|
    loc.lineno >= lineno - lines && loc.lineno <= lineno + lines
  end
end

- (Code) before(lineno, lines = 1)

Remove all lines except for the +lines+ up to and excluding +lineno+.

Parameters:

  • lineno (Integer)
  • lines (Integer) (defaults to: 1)

Returns:



162
163
164
165
166
167
168
# File 'lib/pry/code.rb', line 162

def before(lineno, lines = 1)
  return self unless lineno

  select do |loc|
    loc.lineno >= lineno - lines && loc.lineno < lineno
  end
end

- (Code) between(start_line, end_line = nil)

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.

Parameters:

  • start_line (Range, Integer)
  • end_line (Integer?) (defaults to: nil)

Returns:



129
130
131
132
133
134
135
136
137
# File 'lib/pry/code.rb', line 129

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

- (String) comment_describing(line_number)

Get the comment that describes the expression on the given line number.

Parameters:

  • line_number (Integer)

    (1-based)

Returns:

  • (String)

    the code.



279
280
281
# File 'lib/pry/code.rb', line 279

def comment_describing(line_number)
  self.class.comment_describing(raw, line_number)
end

- (String) expression_at(line_number, consume = 0)

Get the multiline expression that starts on the given line number.

Parameters:

  • line_number (Integer)

    (1-based)

Returns:

  • (String)

    the code.



287
288
289
# File 'lib/pry/code.rb', line 287

def expression_at(line_number, consume = 0)
  self.class.expression_at(raw, line_number, :consume => consume)
end

- (Code) grep(pattern)

Remove all lines that don't match the given pattern.

Parameters:

  • pattern (Regexp)

Returns:



201
202
203
204
205
206
207
208
# File 'lib/pry/code.rb', line 201

def grep(pattern)
  return self unless pattern
  pattern = Regexp.new(pattern)

  select do |loc|
    loc.line =~ pattern
  end
end

- (String) inspect

Returns:

  • (String)


245
246
247
# File 'lib/pry/code.rb', line 245

def inspect
  Object.instance_method(:to_s).bind(self).call
end

- (Integer) length

Return the number of lines stored.

Returns:

  • (Integer)


310
311
312
# File 'lib/pry/code.rb', line 310

def length
  @lines ? @lines.length : 0
end

- (Integer) max_lineno_width

Returns the number of digits in the last line.

Returns:

  • (Integer)

    the number of digits in the last line.



250
251
252
# File 'lib/pry/code.rb', line 250

def max_lineno_width
  @lines.length > 0 ? @lines.last.lineno.to_s.length : 0
end

- (Array<Module>) nesting_at(line_number, top_module = Object)

Get the (approximate) Module.nesting at the give line number.

Parameters:

  • line_number (Integer)

    line number starting from 1

  • top_module (Module) (defaults to: Object)

    the module in which this code exists

Returns:

  • (Array<Module>)

    a list of open modules.



296
297
298
# File 'lib/pry/code.rb', line 296

def nesting_at(line_number, top_module = Object)
  Pry::Indent.nesting_at(raw, line_number)
end

Writes a formatted representation (based on the configuration of the object) to the given output, which must respond to #<<.



262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/pry/code.rb', line 262

def print_to_output(output)
  @lines.each do |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
    output << loc.line
    output << "\n"
  end
  output
end

- (String) push(line, lineno = nil) Also known as: <<

Append the given line. +lineno+ is one more than the last existing line, unless specified otherwise.

Parameters:

  • line (String)
  • lineno (Integer?) (defaults to: nil)

Returns:

  • (String)

    The inserted line.



103
104
105
106
107
108
109
# File 'lib/pry/code.rb', line 103

def push(line, lineno = nil)
  if lineno.nil?
    lineno = @lines.last.lineno + 1
  end
  @lines.push(LOC.new(line, lineno))
  line
end

- (String) raw

Return an unformatted String of the code.

Returns:

  • (String)


303
304
305
# File 'lib/pry/code.rb', line 303

def raw
  @lines.map(&:line).join("\n") << "\n"
end

- (Code) select {|LOC| ... }

Filter the lines using the given block.

Yields:

Returns:



116
117
118
119
120
# File 'lib/pry/code.rb', line 116

def select(&block)
  alter do
    @lines = @lines.select(&block)
  end
end

- (Code) take_lines(start_line, num_lines)

Take num_lines from start_line, forward or backwards.

Parameters:

  • start_line (Integer)
  • num_lines (Integer)

Returns:



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pry/code.rb', line 144

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, 0].max
    end

  alter do
    @lines = @lines.slice(start_idx, num_lines)
  end
end

- (String) to_s

Returns a formatted representation (based on the configuration of the object).

Returns:

  • (String)

    a formatted representation (based on the configuration of the object).



256
257
258
# File 'lib/pry/code.rb', line 256

def to_s
  print_to_output("")
end

- (Code) with_indentation(spaces = 0)

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

Parameters:

  • spaces (Integer?) (defaults to: 0)

Returns:



237
238
239
240
241
242
# File 'lib/pry/code.rb', line 237

def with_indentation(spaces = 0)
  alter do
    @with_indentation = !!spaces
    @indentation_num  = spaces
  end
end

- (Code) with_line_numbers(y_n = true)

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

Parameters:

  • y_n (Boolean?) (defaults to: true)

Returns:



214
215
216
217
218
# File 'lib/pry/code.rb', line 214

def with_line_numbers(y_n = true)
  alter do
    @with_line_numbers = y_n
  end
end

- (Code) with_marker(lineno = 1)

Format output with a marker next to the given +lineno+, unless +lineno+ is falsy.

Parameters:

  • lineno (Integer?) (defaults to: 1)

Returns:



225
226
227
228
229
230
# File 'lib/pry/code.rb', line 225

def with_marker(lineno = 1)
  alter do
    @with_marker   = !!lineno
    @marker_lineno = lineno
  end
end