`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.
Instantiate a `Code` object containing code loaded from a file or Pry's line buffer.
@param [String] fn The name of a file, or "(pry)". @param [Symbol] code_type (:ruby) The type of code the file contains. @return [Code]
# File lib/pry/code.rb, line 36 def from_file(fn, code_type=nil) if fn == Pry.eval_path f = Pry.line_buffer.drop(1) else if File.readable?(fn) f = File.open(fn, 'r') code_type = type_from_filename(fn) else raise CommandError, "Cannot open #{fn.inspect} for reading." end end new(f, 1, code_type || :ruby) ensure f.close if f.respond_to?(:close) end
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 [Fixnum, nil] The line number to start on, or nil to use the
method's original line numbers.
@return [Code]
# File lib/pry/code.rb, line 60 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
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 [Fixnum?] (1) start_line @param [Symbol?] (:ruby) code_type
# File lib/pry/code.rb, line 108 def initialize(lines=[], start_line=1, code_type=:ruby) if lines.is_a? String lines = lines.lines end @lines = lines.each_with_index.map { |l, i| [l.chomp, i + start_line.to_i] } @code_type = code_type end
Guess the CodeRay type of a file from its extension, or nil if unknown.
@param [String] filename @return [Symbol, nil]
# File lib/pry/code.rb, line 72 def type_from_filename(filename) map = { %(.c .h) => :c, %(.cpp .hpp .cc .h cxx) => :cpp, %(.rb .ru .irbrc .gemspec .pryrc) => :ruby, %(.py) => :python, %(.diff) => :diff, %(.css) => :css, %(.html) => :html, %(.yaml .yml) => :yaml, %(.xml) => :xml, %(.php) => :php, %(.js) => :javascript, %(.java) => :java, %(.rhtml) => :rhtml, %(.json) => :json } _, type = map.find do |k, _| k.any? { |ext| ext == File.extname(filename) } end type end
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.
@param [Code, Object] other @return [Boolean]
# File lib/pry/code.rb, line 327 def ==(other) if other.is_a?(Code) @other_lines = other.instance_variable_get(:@lines) @lines.each_with_index.all? do |(l, ln), i| l == @other_lines[i].first && ln == @other_lines[i].last end else to_s.chomp == other.to_s.chomp end end
Remove all lines except for the `lines` after and excluding `line_num`.
@param [Fixnum] line_num @param [Fixnum] (1) lines @return [Code]
# File lib/pry/code.rb, line 208 def after(line_num, lines=1) return self unless line_num select do |l, ln| ln > line_num && ln <= line_num + lines end end
Remove all lines except for the `lines` on either side of and including `line_num`.
@param [Fixnum] line_num @param [Fixnum] (1) lines @return [Code]
# File lib/pry/code.rb, line 195 def around(line_num, lines=1) return self unless line_num select do |l, ln| ln >= line_num - lines && ln <= line_num + lines end end
Remove all lines except for the `lines` up to and excluding `line_num`.
@param [Fixnum] line_num @param [Fixnum] (1) lines @return [Code]
# File lib/pry/code.rb, line 181 def before(line_num, lines=1) return self unless line_num select do |l, ln| ln >= line_num - lines && ln < line_num end end
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, Fixnum] start_line @param [Fixnum?] end_line @return [Code]
# File lib/pry/code.rb, line 147 def between(start_line, end_line=nil) return self unless start_line if start_line.is_a? Range end_line = start_line.last end_line -= 1 if start_line.exclude_end? start_line = start_line.first else end_line ||= start_line end if start_line > 0 start_idx = @lines.index { |l| l.last >= start_line } || @lines.length else start_idx = start_line end if end_line > 0 end_idx = (@lines.index { |l| l.last > end_line } || 0) - 1 else end_idx = end_line end alter do @lines = @lines[start_idx..end_idx] || [] end end
Remove all lines that don't match the given `pattern`.
@param [Regexp] pattern @return [Code]
# File lib/pry/code.rb, line 220 def grep(pattern) return self unless pattern pattern = Regexp.new(pattern) select do |l, ln| l =~ pattern end end
@return [String]
# File lib/pry/code.rb, line 264 def inspect Object.instance_method(:to_s).bind(self).call end
Return the number of lines stored.
@return [Fixnum]
# File lib/pry/code.rb, line 318 def length @lines ? @lines.length : 0 end
Forward any missing methods to the output of `to_s`.
# File lib/pry/code.rb, line 339 def method_missing(name, *args, &blk) to_s.send(name, *args, &blk) end
Append the given line. `line_num` is one more than the last existing line, unless specified otherwise.
@param [String] line @param [Fixnum?] line_num @return [String] The inserted line.
# File lib/pry/code.rb, line 123 def push(line, line_num=nil) line_num = @lines.last.last + 1 unless line_num @lines.push([line.chomp, line_num]) line end
Return an unformatted String of the code.
@return [String]
# File lib/pry/code.rb, line 311 def raw @lines.map(&:first).join("\n") + "\n" end
Filter the lines using the given block.
@yield [line] @return [Code]
# File lib/pry/code.rb, line 134 def select(&blk) alter do @lines = @lines.select(&blk) end end
Based on the configuration of the object, return a formatted String representation.
@return [String]
# File lib/pry/code.rb, line 272 def to_s lines = @lines.map(&:dup) if Pry.color lines.each do |l| l[0] = CodeRay.scan(l[0], @code_type).term end end if @with_line_numbers max_width = lines.last.last.to_s.length if lines.length > 0 lines.each do |l| padded_line_num = l[1].to_s.rjust(max_width) l[0] = "#{Pry::Helpers::Text.blue(padded_line_num)}: #{l[0]}" end end if @with_marker lines.each do |l| if l[1] == @marker_line_num l[0] = " => #{l[0]}" else l[0] = " #{l[0]}" end end end if @with_indentation lines.each do |l| l[0] = "#{' ' * @indentation_num}#{l[0]}" end end lines.map { |l| "#{l.first}\n" }.join end
Format output with the specified number of spaces in front of every line, unless `spaces` is falsy.
@param [Fixnum?] (0) spaces @return [Code]
# File lib/pry/code.rb, line 256 def with_indentation(spaces=0) alter do @with_indentation = !!spaces @indentation_num = spaces end end
Format output with line numbers next to it, unless `y_n` is falsy.
@param [Boolean?] (true) y_n @return [Code]
# File lib/pry/code.rb, line 233 def with_line_numbers(y_n=true) alter do @with_line_numbers = y_n end end
Format output with a marker next to the given `line_num`, unless `line_num` is falsy.
@param [Fixnum?] (1) line_num @return [Code]
# File lib/pry/code.rb, line 244 def with_marker(line_num=1) alter do @with_marker = !!line_num @marker_line_num = line_num end end
Generated with the Darkfish Rdoc Generator 2.