Class: Pry::Code
- Extended by:
- MethodSource::CodeHelpers
- Defined in:
- lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code/loc.rb,
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/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
Instance Attribute Summary collapse
-
#code_type ⇒ Symbol
The type of code stored in this wrapper.
Class Method Summary collapse
-
.from_file(filename, code_type = nil) ⇒ Code
Instantiate a ‘Code` object containing code loaded from a file or Pry’s line buffer.
-
.from_method(meth, start_line = nil) ⇒ Code
Instantiate a ‘Code` object containing code extracted from a `::Method`, `UnboundMethod`, `Proc`, or `Pry::Method` object.
-
.from_module(mod, candidate_rank = 0, start_line = nil) ⇒ Code
Attempt to extract the source code for module (or class) ‘mod`.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Two ‘Code` objects are equal if they contain the same lines with the same numbers.
-
#after(lineno, lines = 1) ⇒ Code
Remove all lines except for the
lines
after and excludinglineno
. -
#around(lineno, lines = 1) ⇒ Code
Remove all lines except for the
lines
on either side of and includinglineno
. -
#before(lineno, lines = 1) ⇒ Code
Remove all lines except for the
lines
up to and excludinglineno
. -
#between(start_line, end_line = nil) ⇒ 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).
-
#comment_describing(line_number) ⇒ String
Get the comment that describes the expression on the given line number.
-
#expression_at(line_number, consume = 0) ⇒ String
Get the multiline expression that starts on the given line number.
-
#grep(pattern) ⇒ Code
Remove all lines that don’t match the given ‘pattern`.
-
#highlighted ⇒ String
A (possibly highlighted) copy of the source code.
-
#initialize(lines = [], start_line = 1, code_type = :ruby) ⇒ Code
constructor
Instantiate a ‘Code` object containing code from the given `Array`, `String`, or `IO`.
-
#length ⇒ Integer
Return the number of lines stored.
-
#max_lineno_width ⇒ Integer
The number of digits in the last line.
-
#method_missing(method_name, *args, &block) ⇒ Object
Forward any missing methods to the output of ‘#to_s`.
-
#nesting_at(line_number) ⇒ Array<Module>
Get the (approximate) Module.nesting at the give line number.
-
#print_to_output(output, color = false) ⇒ Object
Writes a formatted representation (based on the configuration of the object) to the given output, which must respond to ‘#<<`.
-
#push(line) ⇒ void
(also: #<<)
Append the given line.
-
#raw ⇒ String
Return an unformatted String of the code.
-
#reject {|LOC| ... } ⇒ Code
Filter the lines using the given block.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check whether String responds to missing methods.
-
#select {|LOC| ... } ⇒ Code
Filter the lines using the given block.
-
#take_lines(start_line, num_lines) ⇒ Code
Take ‘num_lines` from `start_line`, forward or backwards.
-
#to_s ⇒ String
A formatted representation (based on the configuration of the object).
-
#with_indentation(spaces = 0) ⇒ Code
Format output with the specified number of spaces in front of every line, unless ‘spaces` is falsy.
-
#with_line_numbers(y_n = true) ⇒ Code
Format output with line numbers next to it, unless ‘y_n` is falsy.
-
#with_marker(lineno = 1) ⇒ Code
Format output with a marker next to the given
lineno
, unlesslineno
is falsy.
Methods included from MethodSource::CodeHelpers
Constructor Details
#initialize(lines = [], start_line = 1, code_type = :ruby) ⇒ 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.
87 88 89 90 91 92 93 94 95 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 87 def initialize(lines = [], start_line = 1, code_type = :ruby) lines = lines.lines if lines.is_a? String @lines = lines.each_with_index.map do |line, lineno| LOC.new(line, lineno + start_line.to_i) end @code_type = code_type @with_marker = @with_indentation = @with_line_numbers = nil end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Forward any missing methods to the output of ‘#to_s`.
335 336 337 338 339 340 341 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 335 def method_missing(method_name, *args, &block) if (string = to_s).respond_to?(method_name) string.__send__(method_name, *args, &block) else super end end |
Instance Attribute Details
#code_type ⇒ Symbol
Returns The type of code stored in this wrapper.
77 78 79 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 77 def code_type @code_type end |
Class Method Details
.from_file(filename, code_type = nil) ⇒ Code
Instantiate a ‘Code` object containing code loaded from a file or Pry’s line buffer.
42 43 44 45 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 42 def from_file(filename, code_type = nil) code_file = CodeFile.new(filename, code_type) new(code_file.code, 1, code_file.code_type) end |
.from_method(meth, start_line = nil) ⇒ Code
Instantiate a ‘Code` object containing code extracted from a `::Method`, `UnboundMethod`, `Proc`, or `Pry::Method` object.
55 56 57 58 59 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 55 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 |
.from_module(mod, candidate_rank = 0, start_line = nil) ⇒ Code
Attempt to extract the source code for module (or class) ‘mod`.
69 70 71 72 73 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 69 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
#==(other) ⇒ Boolean
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.
325 326 327 328 329 330 331 332 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 325 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 |
#after(lineno, lines = 1) ⇒ Code
Remove all lines except for the lines
after and excluding lineno
.
195 196 197 198 199 200 201 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 195 def after(lineno, lines = 1) return self unless lineno select do |loc| loc.lineno > lineno && loc.lineno <= lineno + lines end end |
#around(lineno, lines = 1) ⇒ Code
Remove all lines except for the lines
on either side of and including lineno
.
182 183 184 185 186 187 188 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 182 def around(lineno, lines = 1) return self unless lineno select do |loc| loc.lineno >= lineno - lines && loc.lineno <= lineno + lines end end |
#before(lineno, lines = 1) ⇒ Code
Remove all lines except for the lines
up to and excluding lineno
.
168 169 170 171 172 173 174 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 168 def before(lineno, lines = 1) return self unless lineno select do |loc| loc.lineno >= lineno - lines && loc.lineno < lineno end end |
#between(start_line, end_line = nil) ⇒ 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.
135 136 137 138 139 140 141 142 143 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 135 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 |
#comment_describing(line_number) ⇒ String
Get the comment that describes the expression on the given line number.
286 287 288 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 286 def comment_describing(line_number) self.class.comment_describing(raw, line_number) end |
#expression_at(line_number, consume = 0) ⇒ String
Get the multiline expression that starts on the given line number.
294 295 296 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 294 def expression_at(line_number, consume = 0) self.class.expression_at(raw, line_number, consume: consume) end |
#grep(pattern) ⇒ Code
Remove all lines that don’t match the given ‘pattern`.
207 208 209 210 211 212 213 214 215 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 207 def grep(pattern) return self unless pattern pattern = Regexp.new(pattern) select do |loc| loc.line =~ pattern end end |
#highlighted ⇒ String
Returns a (possibly highlighted) copy of the source code.
263 264 265 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 263 def highlighted print_to_output(''.dup, true) end |
#length ⇒ Integer
Return the number of lines stored.
316 317 318 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 316 def length @lines ? @lines.length : 0 end |
#max_lineno_width ⇒ Integer
Returns the number of digits in the last line.
252 253 254 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 252 def max_lineno_width !@lines.empty? ? @lines.last.lineno.to_s.length : 0 end |
#nesting_at(line_number) ⇒ Array<Module>
Get the (approximate) Module.nesting at the give line number.
302 303 304 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 302 def nesting_at(line_number) Pry::Indent.nesting_at(raw, line_number) end |
#print_to_output(output, color = false) ⇒ Object
Writes a formatted representation (based on the configuration of the object) to the given output, which must respond to ‘#<<`.
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 269 def print_to_output(output, color = false) @lines.each do |loc| loc = loc.dup loc.colorize(@code_type) if color loc.add_line_number(max_lineno_width, color) 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 |
#push(line) ⇒ void Also known as: <<
This method returns an undefined value.
Append the given line. lineno
is one more than the last existing line, unless specified otherwise.
102 103 104 105 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 102 def push(line) line_number = @lines.any? ? @lines.last.lineno + 1 : 1 @lines.push(LOC.new(line, line_number)) end |
#raw ⇒ String
Return an unformatted String of the code.
309 310 311 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 309 def raw @lines.map(&:line).join("\n") << "\n" end |
#reject {|LOC| ... } ⇒ Code
Filter the lines using the given block.
122 123 124 125 126 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 122 def reject(&block) alter do @lines = @lines.reject(&block) end end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check whether String responds to missing methods.
345 346 347 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 345 def respond_to_missing?(method_name, include_private = false) ''.respond_to?(method_name, include_private) || super end |
#select {|LOC| ... } ⇒ Code
Filter the lines using the given block.
112 113 114 115 116 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 112 def select(&block) alter do @lines = @lines.select(&block) end end |
#take_lines(start_line, num_lines) ⇒ Code
Take ‘num_lines` from `start_line`, forward or backwards.
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 150 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 |
#to_s ⇒ String
Returns a formatted representation (based on the configuration of the object).
258 259 260 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 258 def to_s print_to_output(''.dup, false) end |
#with_indentation(spaces = 0) ⇒ Code
Format output with the specified number of spaces in front of every line, unless ‘spaces` is falsy.
244 245 246 247 248 249 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 244 def with_indentation(spaces = 0) alter do @with_indentation = !!spaces @indentation_num = spaces end end |
#with_line_numbers(y_n = true) ⇒ Code
Format output with line numbers next to it, unless ‘y_n` is falsy.
221 222 223 224 225 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 221 def with_line_numbers(y_n = true) alter do @with_line_numbers = y_n end end |
#with_marker(lineno = 1) ⇒ Code
Format output with a marker next to the given lineno
, unless lineno
is falsy.
232 233 234 235 236 237 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/code.rb', line 232 def with_marker(lineno = 1) alter do @with_marker = !!lineno @marker_lineno = lineno end end |