Module: Wrong::D

Extended by:
D
Included in:
Object, D, Helpers
Defined in:
lib/wrong/d.rb

Instance Method Summary collapse

Instance Method Details

#d(*args, &block) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wrong/d.rb', line 17

def d(*args, &block)
  called_from = caller.first.split(':')
  chunk = Chunk.from_block(block, 1)
  sexp = chunk.sexp

  # look for a "d" inside the block
  sexp.each_subexp do |subexp|
  #sexp.deep_each do |subexp|  # todo: try to use deep_each
    if subexp.d?
      sexp = subexp[3] # swap in the block part of the nested d call
    end
  end

  code = sexp.to_ruby
  value = eval(code, block.binding, called_from[0], called_from[1].to_i)
  width = Terminal.width
  value = PP.pp(value, "", width - (code.size + 3)).chomp

  if Wrong.config[:color]
    require "wrong/rainbow"
    code = code.color(:blue)
    value = value.color(:magenta)
  end

  message = args.compact.join(", ") + ":" unless args.empty?
  puts [message, code, "is", value].compact.join(" ")
end