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
22 23 24 |
# File 'lib/dbg-rb.rb', line 22 def self.color_code=(val) @@color_code = val end |
.highlight!(wrapper) ⇒ Object
26 27 28 |
# File 'lib/dbg-rb.rb', line 26 def self.highlight!(wrapper) @@highlight = wrapper end |
Instance Method Details
#colorize(str, color_code) ⇒ Object
81 82 83 |
# File 'lib/dbg-rb.rb', line 81 def colorize(str, color_code) "\e[#{color_code}m#{str}\e[0m" end |
#dbg!(*msgs) ⇒ Object
30 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 |
# File 'lib/dbg-rb.rb', line 30 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
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dbg-rb.rb', line 85 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 |