Module: Kernel

Defined in:
lib/kernel.rb

Overview

Overrides for ‘p` and `pp` Kernel methods.

Instance Method Summary collapse

Instance Method Details

#_betterp(raw, args, block, options = {}) ⇒ Object

rubocop:disable Metrics/AbcSize



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kernel.rb', line 15

def _betterp(raw, args, block, options = {}) # rubocop:disable Metrics/AbcSize
  source = Betterp::Source.new(raw, Dir.pwd)
  pretty = options.fetch(:pretty, false)

  block_result, duration = Betterp::TimedBlock.new(block:).result unless block.nil?

  output = Betterp::Output.new(raw, source, duration, pretty:)
  formatted_output = output.formatted(block.nil? ? args : [block_result]).join("\n")

  return formatted_output if Betterp.configuration.test_mode

  $stdout.write(formatted_output)

  return block_result unless block.nil?

  args.size > 1 ? args : args.first
end

#p(*args, &block) ⇒ Object



5
6
7
8
# File 'lib/kernel.rb', line 5

def p(*args, &block)
  raw = caller(1..1).first
  _betterp(raw, args, block)
end

#pp(*args, &block) ⇒ Object



10
11
12
13
# File 'lib/kernel.rb', line 10

def pp(*args, &block)
  raw = caller(1..1).first
  _betterp(raw, args, block, pretty: true)
end