Skip to content

Instantly share code, notes, and snippets.

@davidteren
Created March 22, 2021 10:38
Show Gist options
  • Save davidteren/961e1b5230a7a68687ec0fc86d01eb56 to your computer and use it in GitHub Desktop.
Save davidteren/961e1b5230a7a68687ec0fc86d01eb56 to your computer and use it in GitHub Desktop.
Dev util to show the filename (path), line number, object type and value in the Rails console.
# = SimpleSpy
# Dev util to show the filename (path), line number, object type
# and value in the Rails console.
# === Examples:
# spy val = Object.new
#
# "----------------------------------------------------------------------
# properties.rb:13:in `<main>'+
# Object : #<Object:0x007fb5f28707d0>
# ----------------------------------------------------------------------"
# ---
# spy val = {key: [{key2: "Foo"}, {"key2" => "Bar"}]}
#
# "----------------------------------------------------------------------
# SampleApp/config/initializers/simple_spy.rb:27:in `<main>'
# Hash: {:key=>[{:key2=>"Foo"}, {"key2"=>"Bar"}]}
# ----------------------------------------------------------------------"
# ---
# spy val = nil
#
# "----------------------------------------------------------------------
# SampleApp/config/initializers/simple_spy.rb:29:in `<main>'
# NilClass:
# ----------------------------------------------------------------------"
# ---
# spy val = true
#
# "----------------------------------------------------------------------
# SampleApp/config/initializers/simple_spy.rb:31:in `<main>'
# TrueClass: true
# ----------------------------------------------------------------------"
# ---
# def this_method
# val = "FooBar"
# end
#
# spy this_method
# "----------------------------------------------------------------------
# SampleApp/config/initializers/simple_spy.rb:37:in `<main>'
# String: FooBar
# ----------------------------------------------------------------------"
# ---
# class SomeClass
# def initialize
# @val = method_in_class
# end
#
# def method_in_class
# spy val = 10_000_000
# end
# end
#
# spy val = SomeClass.new
#
# "----------------------------------------------------------------------
# SampleApp/config/initializers/simple_spy.rb:46:in `method_in_class'
# Integer: 10000000
# ----------------------------------------------------------------------"
# "----------------------------------------------------------------------
# SampleApp/config/initializers/simple_spy.rb:54:in `<main>'
# SomeClass: #<SomeClass:0x00007fae4b6367e8>
# ----------------------------------------------------------------------"
# ---
# === Usage:
# Add the following to `config/initializers/simple_spy.`
module SimpleSpy
def spy(msg, len = 80)
print "-" * len + "\n" + path_details(caller) + "\n" + msg.class.to_s
print ": " + msg.to_s + "\n" + "-" * len + "\n" * 2
msg
end
def path_details(c)
root_dir = Rails.root.to_s.split("/").last
File.join(root_dir, c.first.split(root_dir)[1])
end
end
include SimpleSpy if Rails.env.development?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment