im-lost 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39160aff2b7fa8320ae12c5d035fa40fbf6f96a2fbe0418ddfb31ac700c2bf72
4
- data.tar.gz: 044bf4b9b4f9288f418b0cc5344cc82d1a88839d08a8ef1b8008d616ef11481c
3
+ metadata.gz: 5f3add8daa7364ab1a0f1a5058a09b529e448db9bfeda20c9f35e9e69fccbc90
4
+ data.tar.gz: 8bfad384dc5dfd659bb5a49eda611e90b9ee090d0bfa5dfbfd9979289591bac8
5
5
  SHA512:
6
- metadata.gz: 5d76261aafb61289cf56c23fb2acb0c0e6a0b8cc834b4e28b72d80cf0f6dfdf7966a8b613c7a0f4e8ee005eae2ba4064fe9aeb7b7520cbd53ce8e99671e33f0f
7
- data.tar.gz: da598803cf6e2ee4e4baa92f8e702796050f279645daf80fccffa16c2be2eaa314cee7a69742691dde5d798f44f75d610dcb7db71d223a72c0fcabb2e65c7030
6
+ metadata.gz: a2fa68b037ca862fc7754b9f613743e6c358aaf9ec2c299a3dc0b3fc8612ab9537696d21c2212e292530e258470db970f4e6bd2e724c38f1fea22f7b22af3376
7
+ data.tar.gz: 6c47d670e625de3a386cf1412278bb5e6382e8b3809a7bb0678ee7f4ef99983cddfd805655198ad931bb735a336343837c3b4b4e9887ba7bc6dd0064dd168bfc
data/README.md CHANGED
@@ -53,11 +53,28 @@ When you like to know if and when a code point is reached, `ImLost.here` will he
53
53
  ImLost.here
54
54
  ```
55
55
 
56
+ If you like to know the instance variables values of an object, use
57
+ `ImLost.vars`:
58
+
59
+ ```ruby
60
+ ImLost.vars(self)
61
+ ```
62
+
63
+ Or you can print the current local variables:
64
+
65
+ ```ruby
66
+ ImLost.vars(binding)
67
+ ```
68
+
69
+ See the [online help](https://rubydoc.info/gems/im-lost/ImLost) for more!
70
+
56
71
  ## Example
57
72
 
58
73
  ```ruby
59
74
  require 'im-lost'
60
75
 
76
+ require_relative '../lib/im-lost'
77
+
61
78
  class Foo
62
79
  def self.create(value:) = new(value)
63
80
 
@@ -82,43 +99,40 @@ my_foo = Foo.create(value: :foo!)
82
99
  ImLost.trace(my_foo)
83
100
 
84
101
  my_foo.foo(1, key: :none)
102
+ ImLost.vars(my_foo)
103
+
85
104
  my_foo.foo(2, :a, :b, :c, key: :some, name: :value)
105
+ ImLost.vars(my_foo)
106
+
86
107
  my_foo.foo(3) { puts _1 }
108
+ ImLost.vars(my_foo)
87
109
 
88
110
  # output will look like
89
- # > Foo.create(:foo!)
90
- # /projects/foo.rb:25
91
- # > Foo.new(*)
92
- # /projects/foo.rb:6
93
- # < Foo.new(*)
94
- # = #<Foo:0x0000000100902418 @value=:foo!>
95
- # < Foo.create(:foo!)
96
- # = #<Foo:0x0000000100902418 @value=:foo!>
97
- # > Foo#foo(1, *[], :none, **{}, &nil)
98
- # /projects/foo.rb:28
99
- # > Foo#bar()
100
- # /projects/foo.rb:15
101
- # < Foo#bar()
102
- # = :bar
103
- # < Foo#foo(1, *[], :none, **{}, &nil)
104
- # = "1-none-[]-{}-bar"
105
- # > Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
106
- # /projects/foo.rb:29
107
- # > Foo#bar()
108
- # /projects/foo.rb:15
109
- # < Foo#bar()
110
- # = :bar
111
- # < Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
112
- # = "2-some-[a,b,c]-{:name=>:value}-bar"
113
- # > Foo#foo(3, *[], nil, **{}, &#<Proc:0x0000000100900578 /projects/foo.rb:30>)
114
- # /projects/foo.rb:30
115
- # > Foo#bar()
116
- # /projects/foo.rb:15
117
- # < Foo#bar()
118
- # = :bar
119
- # 3--[]-{}-bar
120
- # < Foo#foo(3, *[], nil, **{}, &#<Proc:0x0000000100900578 /projects/foo.rb:30>)
121
- # = nil
111
+ # > Foo.create(:foo!)
112
+ # /projects/foo.rb25
113
+ # > Foo.new(*)
114
+ # /projects/foo.rb6
115
+ # < Foo.new(*)
116
+ # = #<Foo:0x0000000100ab1188 @value=:foo!>
117
+ # < Foo.create(:foo!)
118
+ # = #<Foo:0x0000000100ab1188 @value=:foo!>
119
+ # > Foo#foo(1, *[], :none, **{}, &nil)
120
+ # /projects/foo.rb28
121
+ # > Foo#bar()
122
+ # /projects/foo.rb15
123
+ # < Foo#bar()
124
+ # = :bar
125
+ # < Foo#foo(1, *[], :none, **{}, &nil)
126
+ # = "1-none-[]-{}-bar"
127
+ # = /projects/foo.rb29
128
+ # instance variables:
129
+ # @value: "1-none-[]-{}-bar"
130
+ # = /projects/foo.rb32
131
+ # instance variables:
132
+ # @value: "2-some-[a,b,c]-{:name=>:value}-bar"
133
+ # = /projects/foo.rb35
134
+ # instance variables:
135
+ # @value: "3--[]-{}-bar"
122
136
  ```
123
137
 
124
138
  See [examples dir](./examples) for more…
data/examples/foo.rb CHANGED
@@ -26,40 +26,37 @@ my_foo = Foo.create(value: :foo!)
26
26
  ImLost.trace(my_foo)
27
27
 
28
28
  my_foo.foo(1, key: :none)
29
+ ImLost.vars(my_foo)
30
+
29
31
  my_foo.foo(2, :a, :b, :c, key: :some, name: :value)
32
+ ImLost.vars(my_foo)
33
+
30
34
  my_foo.foo(3) { puts _1 }
35
+ ImLost.vars(my_foo)
31
36
 
32
37
  # output will look like
33
- # > Foo.create(:foo!)
34
- # /projects/foo.rb:25
35
- # > Foo.new(*)
36
- # /projects/foo.rb:6
37
- # < Foo.new(*)
38
- # = #<Foo:0x0000000100902418 @value=:foo!>
39
- # < Foo.create(:foo!)
40
- # = #<Foo:0x0000000100902418 @value=:foo!>
41
- # > Foo#foo(1, *[], :none, **{}, &nil)
42
- # /projects/foo.rb:28
43
- # > Foo#bar()
44
- # /projects/foo.rb:15
45
- # < Foo#bar()
46
- # = :bar
47
- # < Foo#foo(1, *[], :none, **{}, &nil)
48
- # = "1-none-[]-{}-bar"
49
- # > Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
50
- # /projects/foo.rb:29
51
- # > Foo#bar()
52
- # /projects/foo.rb:15
53
- # < Foo#bar()
54
- # = :bar
55
- # < Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
56
- # = "2-some-[a,b,c]-{:name=>:value}-bar"
57
- # > Foo#foo(3, *[], nil, **{}, &#<Proc:0x0000000100900578 /projects/foo.rb:30>)
58
- # /projects/foo.rb:30
59
- # > Foo#bar()
60
- # /projects/foo.rb:15
61
- # < Foo#bar()
62
- # = :bar
63
- # 3--[]-{}-bar
64
- # < Foo#foo(3, *[], nil, **{}, &#<Proc:0x0000000100900578 /projects/foo.rb:30>)
65
- # = nil
38
+ # > Foo.create(:foo!)
39
+ # /projects/foo.rb25
40
+ # > Foo.new(*)
41
+ # /projects/foo.rb6
42
+ # < Foo.new(*)
43
+ # = #<Foo:0x0000000100ab1188 @value=:foo!>
44
+ # < Foo.create(:foo!)
45
+ # = #<Foo:0x0000000100ab1188 @value=:foo!>
46
+ # > Foo#foo(1, *[], :none, **{}, &nil)
47
+ # /projects/foo.rb28
48
+ # > Foo#bar()
49
+ # /projects/foo.rb15
50
+ # < Foo#bar()
51
+ # = :bar
52
+ # < Foo#foo(1, *[], :none, **{}, &nil)
53
+ # = "1-none-[]-{}-bar"
54
+ # = /projects/foo.rb29
55
+ # instance variables:
56
+ # @value: "1-none-[]-{}-bar"
57
+ # = /projects/foo.rb32
58
+ # instance variables:
59
+ # @value: "2-some-[a,b,c]-{:name=>:value}-bar"
60
+ # = /projects/foo.rb35
61
+ # instance variables:
62
+ # @value: "3--[]-{}-bar"
@@ -2,5 +2,5 @@
2
2
 
3
3
  module ImLost
4
4
  # The version number of the gem.
5
- VERSION = '1.0.0'
5
+ VERSION = '1.0.1'
6
6
  end
data/lib/im-lost.rb CHANGED
@@ -1,5 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # If you have overlooked something again and don't really understand what your
4
+ # code is doing. If you have to maintain this application but can't really find
5
+ # your way around and certainly can't track down that stupid error. If you feel
6
+ # lost in all that code, here's the gem to help you out!
7
+ #
8
+ # ImLost helps you by analyzing function calls of objects, informing you about
9
+ # exceptions and logging your way through your code. In short, ImLost is your
10
+ # debugging helper!
11
+ #
3
12
  module ImLost
4
13
  class << self
5
14
  #
@@ -226,6 +235,28 @@ module ImLost
226
235
  self
227
236
  end
228
237
 
238
+ #
239
+ # Inspect internal variables.
240
+ #
241
+ # @overload vars(binding)
242
+ # Inspect local variables of given Binding.
243
+ # @param binding [Binding] which local variables should be print
244
+ # @return [self] itself
245
+ #
246
+ # @overload vars(object)
247
+ # Inspect instance variables of given object.
248
+ # @param object [Object] which instance variables should be print
249
+ # @return [Object] the given object
250
+ #
251
+ def vars(object)
252
+ traced = @trace.delete(object.__id__)
253
+ return _local_vars(object) if object.is_a?(Binding)
254
+ return unless object.respond_to?(:instance_variables)
255
+ _vars(object, Kernel.caller_locations(1, 1)[0])
256
+ ensure
257
+ @trace[traced] = 1 if traced
258
+ end
259
+
229
260
  protected
230
261
 
231
262
  def as_sig(prefix, info, args)
@@ -270,6 +301,34 @@ module ImLost
270
301
  ensure
271
302
  ids.each { @trace.delete(_1) }
272
303
  end
304
+
305
+ def _vars(obj, location)
306
+ @output.puts("= #{location.path}:#{location.lineno}")
307
+ vars = obj.instance_variables
308
+ if vars.empty?
309
+ @output.puts(' <no instance variables defined>')
310
+ else
311
+ @output.puts(' instance variables:')
312
+ vars.sort!.each do |name|
313
+ @output.puts(" #{name}: #{obj.instance_variable_get(name).inspect}")
314
+ end
315
+ end
316
+ obj
317
+ end
318
+
319
+ def _local_vars(binding)
320
+ @output.puts("= #{binding.source_location.join(':')}")
321
+ vars = binding.local_variables
322
+ if vars.empty?
323
+ @output.puts(' <no local variables>')
324
+ else
325
+ @output.puts(' local variables:')
326
+ vars.sort!.each do |name|
327
+ @output.puts(" #{name}: #{binding.local_variable_get(name).inspect}")
328
+ end
329
+ end
330
+ self
331
+ end
273
332
  end
274
333
 
275
334
  ARG_SIG = { rest: '*', keyrest: '**', block: '&' }.compare_by_identity.freeze
@@ -283,12 +342,12 @@ module ImLost
283
342
 
284
343
  @trace_calls = [
285
344
  TracePoint.new(:c_call) do |tp|
286
- next unless @trace.key?(tp.self.__id__)
345
+ next if !@trace.key?(tp.self.__id__) || tp.path == __FILE__
287
346
  @output.puts(as_sig('>', tp, tp.parameters.map { ARG_SIG[_1[0]] || '?' }))
288
347
  @output.puts(" #{tp.path}:#{tp.lineno}") if @caller_locations
289
348
  end,
290
349
  TracePoint.new(:call) do |tp|
291
- next unless @trace.key?(tp.self.__id__)
350
+ next if !@trace.key?(tp.self.__id__) || tp.path == __FILE__
292
351
  ctx = tp.binding
293
352
  @output.puts(
294
353
  as_sig(
@@ -308,12 +367,12 @@ module ImLost
308
367
 
309
368
  @trace_results = [
310
369
  TracePoint.new(:c_return) do |tp|
311
- next unless @trace.key?(tp.self.__id__)
370
+ next if !@trace.key?(tp.self.__id__) || tp.path == __FILE__
312
371
  @output.puts(as_sig('<', tp, tp.parameters.map { ARG_SIG[_1[0]] || '?' }))
313
372
  @output.puts(" = #{tp.return_value.inspect}")
314
373
  end,
315
374
  TracePoint.new(:return) do |tp|
316
- next unless @trace.key?(tp.self.__id__)
375
+ next if !@trace.key?(tp.self.__id__) || tp.path == __FILE__
317
376
  ctx = tp.binding
318
377
  @output.puts(
319
378
  as_sig(
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: im-lost
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  - !ruby/object:Gem::Version
57
57
  version: '0'
58
58
  requirements: []
59
- rubygems_version: 3.5.10
59
+ rubygems_version: 3.2.33
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: Your debugging helper.