SimpleSpy is a handy little method for inspecting an object when developing.
# Simple dev util
# Reveals the filename, line number, object type and value.
module SimpleSpy
def spy(msg, len = 35)
print '-' * len + "\n" + caller[0] + "\n" + msg.class.to_s
print ' : ' + msg.to_s + "\n" + '-' * len + "\n" * 2
end
end
include SimpleSpy
# Examples
spy val = Object.new
spy val = { 'key' => [{ 'key2' => 'Foo' }, { 'key2' => 'Bar' }] }
spy val = nil
spy val = true
def this_method
val = 'FooBar'
end
spy this_method
# In a class
class SomeClass
def initialize
@val = method_in_class
end
def method_in_class
spy val = some_value
end
def some_value
10_000_000
end
end
spy val = SomeClass.new
# -----------------------------------
# properties.rb:13:in `<main>'
# Object : #<Object:0x007fb5f28707d0>
# -----------------------------------
#
# -----------------------------------
# properties.rb:14:in `<main>'
# Hash : {"key"=>[{"key2"=>"Foo"}, {"key2"=>"Bar"}]}
# -----------------------------------
#
# -----------------------------------
# properties.rb:15:in `<main>'
# NilClass :
# -----------------------------------
#
# -----------------------------------
# properties.rb:16:in `<main>'
# TrueClass : true
# -----------------------------------
#
# -----------------------------------
# properties.rb:22:in `<main>'
# String : FooBar
# -----------------------------------
#
# -----------------------------------
# properties.rb:31:in `method_in_class'
# Fixnum : 10000000
# -----------------------------------
#
# -----------------------------------
# properties.rb:39:in `<main>'
# SomeClass : #<SomeClass:0x007fb5f286a5d8>
# -----------------------------------