cutter 0.8.2 → 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +0 -0
- data/README.md +157 -0
- data/lib/cutter.rb +2 -0
- data/lib/cutter/array.rb +0 -0
- data/lib/cutter/colored_outputs.rb +23 -21
- data/lib/cutter/inspection.rb +58 -48
- data/lib/cutter/railtie.rb +0 -0
- data/lib/cutter/stamper.rb +0 -0
- data/lib/cutter/version.rb +3 -0
- metadata +72 -70
- data/README.textile +0 -189
data/LICENSE.txt
CHANGED
File without changes
|
data/README.md
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
# Cutter
|
2
|
+
|
3
|
+
Two-methods-gem I use a lot for simple debugging & performance measuring purposes.
|
4
|
+
|
5
|
+
Include it into Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
group :development, :test do
|
9
|
+
gem 'cutter'
|
10
|
+
end
|
11
|
+
```
|
12
|
+
|
13
|
+
## I) #inspect! (or #iii - it is an alias)
|
14
|
+
|
15
|
+
Insert #inspect! method into any of your methods:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
def your_method *your_args
|
19
|
+
inspect! {} # curly braces are important!
|
20
|
+
...
|
21
|
+
end
|
22
|
+
|
23
|
+
# your_method(1,"foo",:bar) =>
|
24
|
+
|
25
|
+
# method `your_method'
|
26
|
+
# variables:
|
27
|
+
# your_args: [1, "foo", :bar]
|
28
|
+
```
|
29
|
+
|
30
|
+
It gives simple but nice trace for inspection: method's name and args that were passed to method.
|
31
|
+
|
32
|
+
With ```inspect!(:instance){}``` we also see instance variables:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
def instance_demo a, b
|
36
|
+
@instance_var = "blip!"
|
37
|
+
inspect!(:instance){}
|
38
|
+
end
|
39
|
+
|
40
|
+
# instance_demo 1, 2
|
41
|
+
# method: `instance_demo'
|
42
|
+
# called from class: RSpec::Core::ExampleGroup::Nested_1::Nested_1
|
43
|
+
# local_variables:
|
44
|
+
# a: 1
|
45
|
+
# b: 2
|
46
|
+
# instance_variables:
|
47
|
+
# @instance_var: blip!
|
48
|
+
```
|
49
|
+
|
50
|
+
With ```inspect!(:self){}``` we have self#inspect of class to which method belongs to:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
def method_self_inspect name, *args
|
54
|
+
# ...
|
55
|
+
inspect!(:self) {}
|
56
|
+
end
|
57
|
+
|
58
|
+
# method_self_inspect(1,2,3,4,5) =>
|
59
|
+
|
60
|
+
# method: `method_self_inspect'
|
61
|
+
# called from class: SelfInspectDemo
|
62
|
+
# variables:
|
63
|
+
# name: 1
|
64
|
+
# args: [2, 3, 4, 5]
|
65
|
+
# block:
|
66
|
+
# self inspection:
|
67
|
+
# #<SelfInspectDemo:0x82be488 @variable="I'm variable">
|
68
|
+
```
|
69
|
+
|
70
|
+
Option :caller gives us caller methods chain:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
def method_caller_chain name, *args
|
74
|
+
# ...
|
75
|
+
inspect!(:caller)
|
76
|
+
end
|
77
|
+
|
78
|
+
# method_caller_chain(1,2,3,4,5) =>
|
79
|
+
|
80
|
+
# method: `method_caller_chain'
|
81
|
+
# called from class: RSpec::Core::ExampleGroup::Nested_1::Nested_1
|
82
|
+
# variables:
|
83
|
+
# name: 1
|
84
|
+
# args: [2, 3, 4, 5]
|
85
|
+
# block:
|
86
|
+
# caller methods:
|
87
|
+
# /home/stanislaw/_work_/gems/cutter/spec/inspection/demo_spec.rb:33:in `method_caller_chain'
|
88
|
+
# /home/stanislaw/_work_/gems/cutter/spec/inspection/demo_spec.rb:40:in `block (3 levels) in <top (required)>'
|
89
|
+
# /home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `instance_eval'
|
90
|
+
```
|
91
|
+
|
92
|
+
And finally ```inspect!(:max){}``` produces maximum information: options
|
93
|
+
:instance, :self, :caller are included + Ruby's ordinary #inspect method
|
94
|
+
is called on every variable.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
def maximal
|
98
|
+
inspect!(:max){}
|
99
|
+
end
|
100
|
+
|
101
|
+
# maximal(1, :two, "three", :four => 5) =>
|
102
|
+
#
|
103
|
+
# method: `maximal' (maximal tracing)
|
104
|
+
# called from class: RSpec::Core::ExampleGroup::Nested_1::Nested_1
|
105
|
+
# local_variables:
|
106
|
+
# args: [1, :two, "three", {:four=>5}]
|
107
|
+
# instance_variables:
|
108
|
+
# @example: #<RSpec::Core::Example:0xa1d378 >
|
109
|
+
# ...
|
110
|
+
# self inspection:
|
111
|
+
# #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x9e5f8f4
|
112
|
+
# ...
|
113
|
+
# caller methods:
|
114
|
+
# /home/stanislaw/work/gems/cutter/spec/inspection/demo_spec.rb:28:in `maximal'
|
115
|
+
# /home/stanislaw/work/gems/cutter/spec/inspection/demo_spec.rb:54:in `block (3 levels) in <top (required)>'
|
116
|
+
# ...
|
117
|
+
```
|
118
|
+
|
119
|
+
If you want all #inspect! methods fall silent at once, use
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
Cutter::Inspection.quiet!
|
123
|
+
```
|
124
|
+
|
125
|
+
To make them sound again do
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
Cutter::Inspection.loud!
|
129
|
+
```
|
130
|
+
|
131
|
+
You can clone it and try
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
bundle exec rspec spec/inspection/demo_spec.rb
|
135
|
+
```
|
136
|
+
|
137
|
+
Very! Very simple!
|
138
|
+
|
139
|
+
### Notes
|
140
|
+
1. Instead of #inspect! you can use #iii - just an alias more convenient for typing.
|
141
|
+
2. #inspect! colorizes its output. If somebody suggests even better color scheme, I will be thankful.
|
142
|
+
|
143
|
+
## II) #stamper
|
144
|
+
|
145
|
+
Description is coming...
|
146
|
+
|
147
|
+
Acts as self.benchmark{} (in Rails) or Benchmark.measure{} (common Ruby) but with stamps in any position in block executed.
|
148
|
+
It is much simpler to write it quickly than all these Measure-dos.
|
149
|
+
|
150
|
+
## Contributors
|
151
|
+
|
152
|
+
* Stanislaw Pankevich
|
153
|
+
* Kristian Mandrup
|
154
|
+
|
155
|
+
## Copyright
|
156
|
+
|
157
|
+
Copyright (c) 2011 Stanislaw Pankevich
|
data/lib/cutter.rb
CHANGED
data/lib/cutter/array.rb
CHANGED
File without changes
|
@@ -29,28 +29,30 @@ module Cutter
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def colors_config
|
32
|
-
@colors ||= {
|
33
|
-
|
32
|
+
@colors ||= {
|
33
|
+
:line => :blue,
|
34
|
+
:time => :light_blue,
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
36
|
+
:message_name => :cyan,
|
37
|
+
:message_line => :cyan,
|
38
|
+
:total_line => :yellow,
|
39
|
+
:total_count => :yellow,
|
40
|
+
|
41
|
+
# Colors for #inspect!
|
42
|
+
# :called_from => :light_magenta,
|
43
|
+
:class_name => :light_green,
|
44
|
+
:method => :red,
|
45
|
+
:method_name => :yellow,
|
46
|
+
# :lv => :blue,
|
47
|
+
# :lv_names => :magenta,
|
48
|
+
# :lv_values => :light_red,
|
49
|
+
# :iv => :cyan,
|
50
|
+
:iv_names => :cyan,
|
51
|
+
:iv_values => :light_blue,
|
52
|
+
:self_inspection => :red,
|
53
|
+
:self_inspection_trace => :blue,
|
54
|
+
:caller_methods => :red,
|
55
|
+
:caller_method => :green
|
54
56
|
}
|
55
57
|
end
|
56
58
|
end
|
data/lib/cutter/inspection.rb
CHANGED
@@ -29,92 +29,102 @@ class Object
|
|
29
29
|
raise ArgumentError, "Try inspect(binding) or inspect! {}", caller if (!block_given? && !_binding)
|
30
30
|
_binding ||= block.binding
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
_caller = caller
|
32
|
+
max = true if options.include? :max
|
33
|
+
options << :instance << :max << :self << :caller if max
|
34
|
+
options.uniq!
|
35
|
+
|
36
|
+
iv = true if options.include? :instance
|
37
|
+
|
38
|
+
# Want caller methods chain to be traced? - pass option :caller to #inspect!
|
39
|
+
_caller = true if options.include? :caller
|
40
40
|
|
41
|
-
self_inspection = eval('self.inspect', _binding) if options
|
41
|
+
self_inspection = eval('self.inspect', _binding) if options.include? :self
|
42
42
|
|
43
43
|
# Basic info
|
44
44
|
method_name = eval('__method__', _binding)
|
45
45
|
class_name = eval('self.class', _binding)
|
46
46
|
|
47
|
-
puts "\n%s `%s' %s" % ['method:'.
|
48
|
-
puts " %s %s" % ['called from class:'.
|
47
|
+
puts "\n%s `%s' %s" % ['method:'.to_colorized_string(:method), method_name.to_colorized_string(:method_name), ('(maximal tracing)' if max)]
|
48
|
+
puts " %s %s" % ['called from class:'.to_colorized_string(:called_from), class_name.to_colorized_string(:class_name)]
|
49
49
|
|
50
50
|
# Local Variables
|
51
51
|
lvb = eval('local_variables',_binding)
|
52
|
-
puts " %s %s" % ['local_variables:'.
|
52
|
+
puts " %s %s" % ['local_variables:'.to_colorized_string(:lv), ("[]" if lvb.empty?)]
|
53
53
|
|
54
54
|
lvb.map do |lv|
|
55
55
|
local_variable = eval(lv.to_s, _binding)
|
56
|
-
local_variable = (max ? local_variable.inspect : local_variable.
|
56
|
+
local_variable = (max ? local_variable.inspect : local_variable.to_real_string)
|
57
57
|
|
58
|
-
puts " %s: %s" % [lv.
|
58
|
+
puts " %s: %s" % [lv.to_colorized_string(:lv_names), local_variable.to_colorized_string(:lv_values)]
|
59
59
|
end if lvb
|
60
60
|
|
61
61
|
# Instance Variables
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
62
|
+
begin
|
63
|
+
ivb = eval('instance_variables',_binding)
|
64
|
+
|
65
|
+
puts " %s %s" % ["instance_variables:".to_colorized_string(:iv), ("[]" if ivb.empty?)]
|
66
|
+
|
67
|
+
ivb.map do |iv|
|
68
|
+
instance_variable = eval(iv.to_s, _binding)
|
69
|
+
instance_variable = (max ? instance_variable.inspect : instance_variable.to_real_string)
|
70
|
+
|
71
|
+
puts " %s: %s" % [iv.to_colorized_string(:iv_names), instance_variable.to_colorized_string(:iv_values)]
|
72
|
+
end if ivb
|
73
|
+
end if iv
|
72
74
|
|
73
75
|
# Self inspection
|
74
76
|
begin
|
75
|
-
puts " self inspection:".
|
76
|
-
puts "
|
77
|
+
puts " self inspection:".to_colorized_string(:self_inspection)
|
78
|
+
puts " %s" % self_inspection.to_colorized_string(:self_inspection_trace)
|
77
79
|
end if self_inspection
|
78
80
|
|
79
81
|
# Caller methods chain
|
80
82
|
begin
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
end if
|
83
|
+
puts " caller methods: ".to_colorized_string(:caller_methods)
|
84
|
+
caller.each do |meth|
|
85
|
+
puts " %s" % meth.to_colorized_string(:caller_method)
|
86
|
+
end
|
87
|
+
end if _caller
|
86
88
|
|
87
89
|
puts "\n"
|
90
|
+
|
88
91
|
# Yield mysterious things if they exist in block.
|
89
|
-
yield if block_given?
|
92
|
+
yield if block_given?
|
90
93
|
end
|
91
94
|
|
92
|
-
|
95
|
+
alias :iii :inspect!
|
93
96
|
|
94
|
-
|
95
|
-
caller[level][/`([^']*)'/,1].to_sym
|
96
|
-
end
|
97
|
-
|
98
|
-
def maximize_options!
|
99
|
-
self.merge!({:max => true, :inspect => true, :level => 2})
|
100
|
-
end
|
97
|
+
protected
|
101
98
|
|
102
|
-
#
|
103
|
-
def
|
99
|
+
# "Real string". It is now used to print Symbols with colons
|
100
|
+
def to_real_string
|
104
101
|
return ":#{self.to_s}" if self.class == Symbol
|
105
|
-
|
102
|
+
to_s
|
106
103
|
end
|
107
104
|
|
108
|
-
def
|
109
|
-
|
105
|
+
def to_colorized_string obj
|
106
|
+
colors = Cutter::ColoredOutputs.colors_config
|
107
|
+
color = colors[obj] || :default
|
110
108
|
color != :default ? to_s.send(color) : to_s
|
111
109
|
end
|
110
|
+
end
|
112
111
|
|
113
|
-
|
112
|
+
class Object
|
113
|
+
def rrr object = nil
|
114
|
+
raise object.inspect
|
115
|
+
end
|
114
116
|
|
115
|
-
def
|
116
|
-
|
117
|
+
def ppp object = nil
|
118
|
+
puts object.inspect
|
117
119
|
end
|
118
120
|
|
121
|
+
def lll object = nil
|
122
|
+
Rails.logger.info object.inspect
|
123
|
+
end if defined? Rails
|
119
124
|
end
|
120
125
|
|
126
|
+
# def caller_method_name(level = 1)
|
127
|
+
# caller[level][/`([^']*)'/,1].to_sym
|
128
|
+
# end
|
129
|
+
|
130
|
+
|
data/lib/cutter/railtie.rb
CHANGED
File without changes
|
data/lib/cutter/stamper.rb
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,121 +1,123 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: cutter
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.3
|
4
5
|
prerelease:
|
5
|
-
version: 0.8.2
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- stanislaw
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-05-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: activesupport
|
18
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
23
21
|
version: 2.3.5
|
24
22
|
type: :runtime
|
25
23
|
prerelease: false
|
26
|
-
version_requirements:
|
27
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.3.5
|
30
|
+
- !ruby/object:Gem::Dependency
|
28
31
|
name: colorize
|
29
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
30
33
|
none: false
|
31
|
-
requirements:
|
32
|
-
- -
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version:
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0.5'
|
35
38
|
type: :runtime
|
36
39
|
prerelease: false
|
37
|
-
version_requirements:
|
38
|
-
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0.5'
|
46
|
+
- !ruby/object:Gem::Dependency
|
39
47
|
name: bundler
|
40
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
41
49
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version:
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
46
54
|
type: :development
|
47
55
|
prerelease: false
|
48
|
-
version_requirements:
|
49
|
-
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
50
63
|
name: jeweler
|
51
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
52
65
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
version:
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
57
70
|
type: :development
|
58
71
|
prerelease: false
|
59
|
-
version_requirements:
|
60
|
-
- !ruby/object:Gem::Dependency
|
61
|
-
name: rcov
|
62
|
-
requirement: &id005 !ruby/object:Gem::Requirement
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
73
|
none: false
|
64
|
-
requirements:
|
65
|
-
- -
|
66
|
-
- !ruby/object:Gem::Version
|
67
|
-
version:
|
68
|
-
type: :development
|
69
|
-
prerelease: false
|
70
|
-
version_requirements: *id005
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
71
78
|
description: longer description of your gem
|
72
79
|
email: s.pankevich@gmail.com
|
73
80
|
executables: []
|
74
|
-
|
75
81
|
extensions: []
|
76
|
-
|
77
|
-
extra_rdoc_files:
|
82
|
+
extra_rdoc_files:
|
78
83
|
- LICENSE.txt
|
79
|
-
- README.
|
80
|
-
files:
|
84
|
+
- README.md
|
85
|
+
files:
|
81
86
|
- lib/cutter.rb
|
82
87
|
- lib/cutter/array.rb
|
83
88
|
- lib/cutter/colored_outputs.rb
|
84
89
|
- lib/cutter/inspection.rb
|
85
90
|
- lib/cutter/railtie.rb
|
86
91
|
- lib/cutter/stamper.rb
|
92
|
+
- lib/cutter/version.rb
|
87
93
|
- LICENSE.txt
|
88
|
-
- README.
|
89
|
-
has_rdoc: true
|
94
|
+
- README.md
|
90
95
|
homepage: http://github.com/stanislaw/cutter
|
91
|
-
licenses:
|
96
|
+
licenses:
|
92
97
|
- MIT
|
93
98
|
post_install_message:
|
94
99
|
rdoc_options: []
|
95
|
-
|
96
|
-
require_paths:
|
100
|
+
require_paths:
|
97
101
|
- lib
|
98
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
103
|
none: false
|
100
|
-
requirements:
|
101
|
-
- -
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
|
104
|
-
segments:
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
segments:
|
105
109
|
- 0
|
106
|
-
|
107
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
hash: -579611375
|
111
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
112
|
none: false
|
109
|
-
requirements:
|
110
|
-
- -
|
111
|
-
- !ruby/object:Gem::Version
|
112
|
-
version:
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
113
117
|
requirements: []
|
114
|
-
|
115
118
|
rubyforge_project:
|
116
|
-
rubygems_version: 1.
|
119
|
+
rubygems_version: 1.8.19
|
117
120
|
signing_key:
|
118
121
|
specification_version: 3
|
119
122
|
summary: one-line summary of your gem
|
120
123
|
test_files: []
|
121
|
-
|
data/README.textile
DELETED
@@ -1,189 +0,0 @@
|
|
1
|
-
h1. Cutter
|
2
|
-
|
3
|
-
Two-methods-gem I use a lot for simple debugging & performance measuring purposes.
|
4
|
-
|
5
|
-
Include it into Gemfile:
|
6
|
-
|
7
|
-
<pre>
|
8
|
-
<code>
|
9
|
-
gem 'cutter'
|
10
|
-
</code>
|
11
|
-
</pre>
|
12
|
-
|
13
|
-
h2. I) #inspect!
|
14
|
-
|
15
|
-
Insert #inspect! method into any of your methods:
|
16
|
-
|
17
|
-
<pre>
|
18
|
-
<code>
|
19
|
-
def your_method *your_args
|
20
|
-
inspect! {} # this string exactly!
|
21
|
-
...
|
22
|
-
end
|
23
|
-
|
24
|
-
your_method(1,"foo",:bar) =>
|
25
|
-
|
26
|
-
method `your_method'
|
27
|
-
variables:
|
28
|
-
args: [1, "foo", :bar]
|
29
|
-
</code>
|
30
|
-
</pre>
|
31
|
-
|
32
|
-
or in more rigorous way:
|
33
|
-
|
34
|
-
<pre>
|
35
|
-
<code>
|
36
|
-
def your_another_method(first, second, third, &block)
|
37
|
-
inspect!(binding)
|
38
|
-
end
|
39
|
-
|
40
|
-
your_another_method(1,"foo",:bar) =>
|
41
|
-
|
42
|
-
method `your_another_method'
|
43
|
-
variables:
|
44
|
-
first: 1
|
45
|
-
second: "foo"
|
46
|
-
third: :bar
|
47
|
-
block:
|
48
|
-
</code>
|
49
|
-
</pre>
|
50
|
-
|
51
|
-
Gives simple but nice trace for inspection: method's name and args that were passed to method
|
52
|
-
|
53
|
-
With :inspect => true we have self#inspect of class to which method belongs to:
|
54
|
-
|
55
|
-
<pre>
|
56
|
-
<code>
|
57
|
-
def method_self_inspect
|
58
|
-
...
|
59
|
-
inspect!(:inspect => true) {}
|
60
|
-
end
|
61
|
-
|
62
|
-
method_self_inspect(1,2,3,4,5) =>
|
63
|
-
|
64
|
-
method: `method_self_inspect'
|
65
|
-
called from class: SelfInspectDemo
|
66
|
-
variables:
|
67
|
-
name: 1
|
68
|
-
args: [2, 3, 4, 5]
|
69
|
-
block:
|
70
|
-
self inspection:
|
71
|
-
#<SelfInspectDemo:0x82be488 @variable="I'm variable">
|
72
|
-
</code>
|
73
|
-
</pre>
|
74
|
-
|
75
|
-
And finally :level => N gives caller methods chain (N + 1 caller methods):
|
76
|
-
|
77
|
-
<pre>
|
78
|
-
<code>
|
79
|
-
def method_caller_chain
|
80
|
-
...
|
81
|
-
inspect!(:level => 2)
|
82
|
-
end
|
83
|
-
|
84
|
-
method_caller_chain(1,2,3,4,5) =>
|
85
|
-
|
86
|
-
method: `method_caller_chain'
|
87
|
-
called from class: RSpec::Core::ExampleGroup::Nested_1::Nested_1
|
88
|
-
variables:
|
89
|
-
name: 1
|
90
|
-
args: [2, 3, 4, 5]
|
91
|
-
block:
|
92
|
-
caller methods:
|
93
|
-
/home/stanislaw/_work_/gems/cutter/spec/inspection/demo_spec.rb:33:in `method_caller_chain'
|
94
|
-
/home/stanislaw/_work_/gems/cutter/spec/inspection/demo_spec.rb:40:in `block (3 levels) in <top (required)>'
|
95
|
-
/home/stanislaw/.rvm/gems/ruby-1.9.2-p180@310/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `instance_eval'
|
96
|
-
</code>
|
97
|
-
</pre>
|
98
|
-
|
99
|
-
If you want all #inspect! methods fall silent at once, use
|
100
|
-
@Cutter::Inspection.quiet!@
|
101
|
-
To make them sound again do
|
102
|
-
@Cutter::Inspection.loud!@
|
103
|
-
|
104
|
-
You can clone it and try
|
105
|
-
|
106
|
-
@bundle exec rspec spec/inspection/demo_spec.rb@
|
107
|
-
|
108
|
-
Very! Very simple!
|
109
|
-
|
110
|
-
h2. II) #stamper
|
111
|
-
|
112
|
-
<pre>
|
113
|
-
<code>
|
114
|
-
Stamper.scope :stan => "testing test_method" do |stan|
|
115
|
-
stan.msg _1: 'stamp1'
|
116
|
-
stan.msg _2: 'stamp2'
|
117
|
-
stan.msg _3: 'stamp3'
|
118
|
-
end
|
119
|
-
|
120
|
-
Stamper.scope :hello => "testing hello part" do |stan|
|
121
|
-
stan.msg _1: 'hello stan'
|
122
|
-
stan.msg _2: 'nice to see you'
|
123
|
-
end
|
124
|
-
|
125
|
-
v = 32
|
126
|
-
|
127
|
-
stamper :stan do |s|
|
128
|
-
s.stamp
|
129
|
-
s.stamp :_1
|
130
|
-
sleep 0.1
|
131
|
-
s.stamp :_2
|
132
|
-
s.stamp :hello_world
|
133
|
-
stamper :hello do |s|
|
134
|
-
s.stamp :_1
|
135
|
-
sleep 0.1
|
136
|
-
s.stamp :_2
|
137
|
-
end
|
138
|
-
sleep 0.1
|
139
|
-
s.stamp "hello world: #{v}" # access var before block
|
140
|
-
s.stamp :_3
|
141
|
-
sleep 0.1
|
142
|
-
end
|
143
|
-
|
144
|
-
=>
|
145
|
-
|
146
|
-
------------------------------
|
147
|
-
~ START "testing test_method"
|
148
|
-
~ stamp: 0 ms
|
149
|
-
~ stamp: 0 ms stamp1
|
150
|
-
~ stamp: 101 ms stamp2
|
151
|
-
~ stamp: 101 ms Hello world
|
152
|
-
------------------------------
|
153
|
-
~ START "testing hello part"
|
154
|
-
~ stamp: 0 ms hello stan
|
155
|
-
~ stamp: 101 ms nice to see you
|
156
|
-
~ END "testing hello part"
|
157
|
-
[101ms]
|
158
|
-
------------------------------
|
159
|
-
~ stamp: 305 ms Hello world: 32
|
160
|
-
~ stamp: 305 ms stamp3
|
161
|
-
~ END "testing test_method"
|
162
|
-
[406ms]
|
163
|
-
------------------------------
|
164
|
-
</code>
|
165
|
-
</pre>
|
166
|
-
|
167
|
-
or simply:
|
168
|
-
<pre>
|
169
|
-
<code>
|
170
|
-
def test_method
|
171
|
-
stamper {
|
172
|
-
piece of code
|
173
|
-
}
|
174
|
-
end
|
175
|
-
</code>
|
176
|
-
</pre>
|
177
|
-
|
178
|
-
Acts as self.benchmark{} (in Rails) or Benchmark.measure{} (common Ruby) but with stamps in any position in block executed.
|
179
|
-
It is much simpler to write it quickly than all these Measure-dos.
|
180
|
-
|
181
|
-
h2. Contributors
|
182
|
-
|
183
|
-
* Stanislaw Pankevich
|
184
|
-
* Kristian Mandrup
|
185
|
-
|
186
|
-
h2. Copyright
|
187
|
-
|
188
|
-
Copyright (c) 2011 Stanislaw Pankevich
|
189
|
-
|