Class: DbgRb::Impl
- Inherits:
-
Object
- Object
- DbgRb::Impl
- Defined in:
- lib/dbg-rb.rb
Constant Summary collapse
- @@color_code =
nil- @@highlight =
false
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.color_code=(val) ⇒ Object
23 24 25 |
# File 'lib/dbg-rb.rb', line 23 def self.color_code=(val) @@color_code = val end |
.highlight!(wrapper) ⇒ Object
27 28 29 |
# File 'lib/dbg-rb.rb', line 27 def self.highlight!(wrapper) @@highlight = wrapper end |
Instance Method Details
#colorize(str, color_code) ⇒ Object
82 83 84 |
# File 'lib/dbg-rb.rb', line 82 def colorize(str, color_code) "\e[#{color_code}m#{str}\e[0m" end |
#dbg!(*msgs) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/dbg-rb.rb', line 31 def dbg!(*msgs) loc = caller_locations.first(3).last.to_s matching_loc = loc.match(/.+(rb)\:\d+\:(in)\s/) src = if !matching_loc.nil? matching_loc[0][0..-5] else loc end file, line = src.split(":") file = file.split("/").last(2).join("/") src = "[#{file}:#{line}]" msgs.each_with_index do |obj, i| first = i == 0 last = i == (msgs.size - 1) val = if obj.is_a?(Symbol) begin val = binding.of_caller(4).local_variable_get(obj) val = format_val(val) "#{obj} = #{val}" rescue NameError ":#{obj}" end else format_val(obj) end output = "#{src} #{val}" if @@highlight if first output = "#{@@highlight}\n#{output}" end if last output = "#{output}\n#{@@highlight}" end end if @@color_code != nil output = colorize(output, @@color_code) end puts output end nil end |
#format_val(val) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/dbg-rb.rb', line 86 def format_val(val) if val.nil? "nil" elsif val.is_a?(String) "\"#{val}\"" elsif val.is_a?(Hash) || val.is_a?(Array) JSON.pretty_generate(val) else val end end |