class IRB::ExtendCommand::ShowSource

Public Class Methods

transform_args(args) click to toggle source
# File irb/cmd/show_source.rb, line 14
def transform_args(args)
  # Return a string literal as is for backward compatibility
  if args.empty? || string_literal?(args)
    args
  else # Otherwise, consider the input as a String for convenience
    args.strip.dump
  end
end

Public Instance Methods

execute(str = nil) click to toggle source
# File irb/cmd/show_source.rb, line 24
def execute(str = nil)
  unless str.is_a?(String)
    puts "Error: Expected a string but got #{str.inspect}"
    return
  end

  source = SourceFinder.new(@irb_context).find_source(str)

  if source
    show_source(source)
  else
    puts "Error: Couldn't locate a definition for #{str}"
  end
  nil
end

Private Instance Methods

bold(str) click to toggle source
# File irb/cmd/show_source.rb, line 51
def bold(str)
  Color.colorize(str, [:BOLD])
end
show_source(source) click to toggle source
# File irb/cmd/show_source.rb, line 42
def show_source(source)
  puts
  puts "#{bold("From")}: #{source.file}:#{source.first_line}"
  puts
  code = IRB::Color.colorize_code(File.read(source.file))
  puts code.lines[(source.first_line - 1)...source.last_line].join
  puts
end