Skip to content

mblumtritt/im-lost

Repository files navigation

ImLost version

If you have overlooked something again and don't really understand what your code is doing. If you have to maintain this application but can't really find your way around and certainly can't track down that stupid error. If you feel lost in all that code, here's the gem to help you out!

ImLost helps you by analyzing function calls of objects, informing you about exceptions and logging your way through your code. In short, ImLost is your debugging helper!

Description

If you like to undertsand method call details you get a call trace with ImLost.trace:

File.open('test.txt', 'w') do |file|
  ImLost.trace(file) do
    file << 'hello '
    file.puts(:world!)
  end
end

# output will look like
#  > IO#<<(?)
#    /examples/test.rb:1
#  > IO#write(*)
#    /examples/test.rb:1
#  > IO#puts(*)
#    /examples/test.rb:2
#  > IO#write(*)
#    /examples/test.rb:2

When you need to know if exceptions are raised and handled you can use ImLost.trace_exceptions:

ImLost.trace_exceptions do
  File.write('/', 'test')
rescue SystemCallError
  raise('something went wrong!')
end

# output will look like
#  x Errno::EEXIST: File exists @ rb_sysopen - /
#    /examples/test.rb:2
#  ! Errno::EEXIST: File exists @ rb_sysopen - /
#    /examples/test.rb:3
#  x RuntimeError: something went wrong!
#    /examples/test.rb:4

When you like to know if a code point is reached, ImLost.here will help:

ImLost.here

If you like to know the instance variables values of an object, use ImLost.vars:

ImLost.vars(self)

Or you can print the current local variables:

ImLost.vars(binding)

See the online help for more!

Example

require 'im-lost'

require_relative '../lib/im-lost'

class Foo
  def self.create(value:) = new(value)

  attr_reader :value

  def initialize(value)
    @value = value
  end

  def foo(arg, *args, key: nil, **kw_args, &block)
    @value = "#{arg}-#{key}-[#{args.join(',')}]-#{kw_args.inspect}-#{bar}"
    block ? block.call(@value) : @value
  end

  def bar = :bar
end

ImLost.trace(Foo)

my_foo = Foo.create(value: :foo!)
ImLost.trace(my_foo)

my_foo.foo(1, key: :none)
ImLost.vars(my_foo)

my_foo.foo(2, :a, :b, :c, key: :some, name: :value)
ImLost.vars(my_foo)

my_foo.foo(3) { puts _1 }
ImLost.vars(my_foo)

# output will look like
#   > Foo.create(:foo!)
#     /examples/foo.rb:24
#   > Foo.new(*)
#     /examples/foo.rb:6
#   < Foo.new(*)
#     /examples/foo.rb:6
#     = #<Foo:0x00000001006448c0 @value=:foo!>
#   < Foo.create(:foo!)
#     /examples/foo.rb:24
#     = #<Foo:0x00000001006448c0 @value=:foo!>
#   > Foo#foo(1, *[], :none, **{}, &nil)
#     /examples/foo.rb:27
#   > Foo#bar()
#     /examples/foo.rb:15
#   < Foo#bar()
#     /examples/foo.rb:15
#     = :bar
#   < Foo#foo(1, *[], :none, **{}, &nil)
#     /examples/foo.rb:27
#     = "1-none-[]-{}-bar"
#   * /examples/foo.rb:28
#     > instance variables
#       @value: "1-none-[]-{}-bar"
#   > Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
#     /examples/foo.rb:30
#   > Foo#bar()
#     /examples/foo.rb:15
#   < Foo#bar()
#     /examples/foo.rb:15
#     = :bar
#   < Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
#     /examples/foo.rb:30
#     = "2-some-[a,b,c]-{:name=>:value}-bar"
#   * /examples/foo.rb:31
#     > instance variables
#       @value: "2-some-[a,b,c]-{:name=>:value}-bar"
#   > Foo#foo(3, *[], nil, **{}, &#<Proc:0x0000000100641d28 /examples/foo.rb:33>)
#     /examples/foo.rb:33
#   > Foo#bar()
#     /examples/foo.rb:15
#   < Foo#bar()
#     /examples/foo.rb:15
#     = :bar
#   3--[]-{}-bar
#   < Foo#foo(3, *[], nil, **{}, &#<Proc:0x0000000100641d28 /examples/foo.rb:33>)
#     /examples/foo.rb:33
#     = nil
#   * /examples/foo.rb:34
#     > instance variables
#       @value: "3--[]-{}-bar"

See examples dir for more…

Installation

You can install the gem in your system with

gem install im-lost

or you can use Bundler to add ImLost to your own project:

bundle add im-lost

After that you only need one line of code to have everything together

require 'im-lost'