Last active
August 28, 2025 08:33
-
-
Save JoshCheek/55b53e2faa2776d6a054 to your computer and use it in GitHub Desktop.
Revisions
-
JoshCheek revised this gist
Mar 4, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
LoadingSorry, something went wrong. Reload?Sorry, we cannot display this file.Sorry, this file is invalid so it cannot be displayed. -
JoshCheek created this gist
Mar 4, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,71 @@ # https://minhajuddin.com/2016/03/03/put-this-in-your-code-to-debug-anything require 'rouge' require 'method_source' require 'pp' class Dbg def initialize(object, to:) @object, @stream = object, to end def call stream.puts bannerize highlight object.pretty_inspect.chomp end def bannerize(text) color = rand 8 "\e[3#{color}m>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\e[39m\n" + text.chomp + "\n" + "\e[3#{color}m<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\e[39m\n" end private attr_reader :object, :stream def highlight(raw_code) formatter = Rouge::Formatters::Terminal256.new theme: 'base16.solarized.dark' lexer = Rouge::Lexers::Ruby.new tokens = lexer.lex raw_code.chomp formatter.format(tokens) end end class BlockDbg < Dbg alias block object def to_proc Proc.new do |*arguments, &b| stream.puts bannerize highlight(formatted_locals + block.source) block.call(*arguments, &b) end end private def formatted_locals binding = block.binding locals = binding.local_variables.map do |name| [name, binding.local_variable_get(name)] end width = locals.map { |k, v| k.length }.max format_str = "%-#{width}s = %p\n" locals.map { |name, value| sprintf format_str, name, value }.join end end class Object def dbg(&block) block and return BlockDbg.new block, to: $stderr Dbg.new(self, to: $stderr).call self end end def self.get_csv [ {user_id: 100, name: 'Josh'}, {user_id: 200, name: 'Jan' }, {user_id: 300, name: 'Jill'}, ] end row_id = 200 id_column = :user_id get_csv.dbg.find(&dbg { |row| row[id_column] == row_id }).dbg