Module: Kernel

Defined in:
lib/pry-command_result/kernel_ext.rb

Instance Method Summary collapse

Instance Method Details

#command_result(command, target = binding) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pry-command_result/kernel_ext.rb', line 4

def command_result(command, target = binding)
  is_command = Pry.commands.to_a.map(&:first).any? do |cmd|
    case cmd
    when Regexp
      cmd =~ command.split(' ').first
    when String
      cmd == command.split(' ').first
    end
  end

  pager = Pry.config.pager
  color = Pry.config.color
  Pry.config.pager = false
  Pry.config.color = false

  if is_command
    output = StringIO.new
    Pry.run_command(command, output: output, show_output: true)
    output.string
  else
    eval(command, target)
  end
ensure
  Pry.config.color = color
  Pry.config.pager = pager
  output.close if output.respond_to? :close
end