binding-debug 0.1.0 → 0.2.0

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: f20e52b86aa720bfef05903771a5908cd80cee082212a8d87b739528d2b9bbc9
4
- data.tar.gz: 98bd76059c0643eb7dc174e7771a20cd04120ce793816a1e5eb5bab2a4ab0a7c
3
+ metadata.gz: 4dc80b554de30e00e748016f0e0f46bef5acfc7157249560465850b8816fd46c
4
+ data.tar.gz: 4113961c5ed661c20c882c346b4ad88acf6b82e7c2b2d319bf22b5b516dc2633
5
5
  SHA512:
6
- metadata.gz: 75a0a6977fd48b6532aae00d6c7e639e3d011ccd5b6084e54098fec69cd1f96a78f39ce40e2611a1b158fb4fae4ac5a648f50d935fb544897cddbfbd43a3aad1
7
- data.tar.gz: 014da2b8fca57aeed0b04b4cfb820e2892334343a881e9ee12059423047899cbd8b33231073338d7cdb0462a6aa56d48490b4c10f84dab56ecd1b5da18d228f3
6
+ metadata.gz: 0c324146dca90da0221e1961678fee5a4266775fcdb6d69495c65f70703672a34159d825e89a57c1be708618db9191bb2c465fe674eeba4a489b9b0d265f0a4c
7
+ data.tar.gz: 2ff101b9ad0c26d294645c8b1cdf796e55524b42e81ae5d01001622fa1c3a53aaf0075261e4275782dce6210bad97af0aecd65084709ec467d56addc1fb45e4f
@@ -0,0 +1,22 @@
1
+ name: macos
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+ branches:
9
+ - '*'
10
+
11
+ jobs:
12
+ build:
13
+ runs-on: macos-latest
14
+ steps:
15
+ - uses: actions/checkout@master
16
+ - name: Install dependencies
17
+ run: |
18
+ gem install bundler --no-document
19
+ bundle install
20
+ - name: Run test
21
+ run: |
22
+ bundle exec rake spec
@@ -0,0 +1,35 @@
1
+ name: ubuntu-rvm
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+ branches:
9
+ - '*'
10
+
11
+ jobs:
12
+ build:
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ ruby: [ 'ruby-head' ]
17
+ steps:
18
+ - uses: actions/checkout@master
19
+ - name: Set up RVM
20
+ run: |
21
+ curl -sSL https://get.rvm.io | bash
22
+ - name: Set up Ruby
23
+ run: |
24
+ source $HOME/.rvm/scripts/rvm
25
+ rvm install ${{ matrix.ruby }} --binary
26
+ rvm --default use ${{ matrix.ruby }}
27
+ - name: Install dependencies
28
+ run: |
29
+ source $HOME/.rvm/scripts/rvm
30
+ gem install bundler --no-document
31
+ bundle install
32
+ - name: Run test
33
+ run: |
34
+ source $HOME/.rvm/scripts/rvm
35
+ bundle exec rake spec
@@ -0,0 +1,29 @@
1
+ name: ubuntu
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+ branches:
9
+ - '*'
10
+
11
+ jobs:
12
+ build:
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ ruby: [ '2.6.x', '2.5.x' ]
17
+ steps:
18
+ - uses: actions/checkout@master
19
+ - name: Set up Ruby
20
+ uses: actions/setup-ruby@v1
21
+ with:
22
+ version: ${{ matrix.ruby }}
23
+ - name: Install dependencies
24
+ run: |
25
+ gem install bundler --no-document
26
+ bundle install
27
+ - name: Run test
28
+ run: |
29
+ bundle exec rake spec
@@ -0,0 +1,31 @@
1
+ name: windows
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+ branches:
9
+ - '*'
10
+
11
+ jobs:
12
+ build:
13
+ runs-on: windows-latest
14
+ strategy:
15
+ matrix:
16
+ ruby: [ '2.6.x', '2.5.x' ]
17
+ steps:
18
+ - uses: actions/checkout@master
19
+ - name: Set up Ruby
20
+ uses: actions/setup-ruby@v1
21
+ with:
22
+ version: ${{ matrix.ruby }}
23
+ - name: Install dependencies
24
+ run: |
25
+ gem install bundler --no-document
26
+ - name: Bundle install
27
+ run: |
28
+ bundle install
29
+ - name: Run test
30
+ run: |
31
+ bundle exec rake spec
@@ -2,4 +2,5 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.5.0
5
+ - 2.6.0
5
6
  before_install: gem install bundler -v 1.16.1
data/README.md CHANGED
@@ -28,7 +28,7 @@ require "binding/debug"
28
28
  using BindingDebug
29
29
 
30
30
  def plus a, b
31
- a + b
31
+ a + b
32
32
  end
33
33
 
34
34
  #######################################
@@ -65,10 +65,10 @@ binding.p "plus(1, 2)"
65
65
 
66
66
  foo = "homu"
67
67
  binding.puts %{
68
- foo.to_s.upcase
69
- plus 1, 2
70
- (0..20).to_a
71
- foo.class.name
68
+ foo.to_s.upcase
69
+ plus 1, 2
70
+ (0..20).to_a
71
+ foo.class.name
72
72
  }
73
73
  # output:
74
74
  # foo.to_s.upcase : HOMU
@@ -77,6 +77,36 @@ binding.puts %{
77
77
  # foo.class.name : String
78
78
  ```
79
79
 
80
+ ### `puts` with blocks
81
+
82
+ ```ruby
83
+ require "binding/debug"
84
+
85
+ using BindingDebug
86
+
87
+ def plus a, b
88
+ a + b
89
+ end
90
+
91
+ foo = "homu"
92
+
93
+ # puts with blocks
94
+ puts {
95
+ foo.to_s.upcase
96
+ plus 1, 2
97
+ (0..20).to_a
98
+ foo.class.name
99
+ }
100
+ # output:
101
+ # foo.to_s.upcase : HOMU
102
+ # plus 1, 2 : 3
103
+ # (0..20).to_a : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
104
+ # foo.class.name : String
105
+ ```
106
+
107
+ Supported `Kernel.#p` , `Kernel.#pp`.
108
+
109
+
80
110
  ## Development
81
111
 
82
112
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -86,3 +116,19 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
86
116
  ## Contributing
87
117
 
88
118
  Bug reports and pull requests are welcome on GitHub at https://github.com/osyo-manga/gem-binding-debug.
119
+
120
+
121
+ ## Release Note
122
+
123
+ #### 0.2.0
124
+
125
+ * Add `BindingDebug::Formats`
126
+ * Add `Binding#pp`
127
+ * Add with block in `Kernle.#puts` `Kernle.#p` `Kernle.#pp`
128
+ * e.g `puts { value } # => "value : #{value}"`
129
+
130
+ #### 0.1.0
131
+
132
+ * Release!!
133
+
134
+
@@ -20,7 +20,9 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.16"
24
- spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_dependency "binding_of_caller"
24
+
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "rake"
25
27
  spec.add_development_dependency "rspec", "~> 3.0"
26
28
  end
@@ -1,21 +1,133 @@
1
1
  require "binding/debug/version"
2
+ require "binding_of_caller"
3
+ require "pp"
2
4
 
3
5
  module BindingDebug
4
- refine Binding do
5
- def debug expr, &block
6
- block ||= proc { |name, value| "#{name} : #{value}" }
7
- binding = self
8
- expr.split("\n").reject { |it| /^\s*$/ =~ it }.map(&:strip).map { |expr|
9
- block.call expr, binding.eval(expr)
10
- }.join("\n")
11
- end
12
-
13
- def p expr, &block
14
- super debug expr, &block
15
- end
16
-
17
- def puts expr, &block
18
- super debug expr, &block
19
- end
20
- end
6
+ module Formats
7
+ module_function
8
+
9
+ def default
10
+ proc { |name, value| "#{name} : #{value}" }
11
+ end
12
+
13
+ def inspect formatter = default
14
+ proc { |name, value| formatter.call "#{name}.inspect", value.inspect }
15
+ end
16
+
17
+ def prefix str
18
+ proc { |name, value| "#{str} #{BindingDebug::Formats.default.call name, value}" }
19
+ end
20
+
21
+ def suffix str
22
+ proc { |name, value| "#{BindingDebug::Formats.default.call name, value} #{str}" }
23
+ end
24
+ end
25
+
26
+ refine Binding do
27
+ def debug expr, &block
28
+ block ||= Formats.default
29
+ binding = self
30
+ expr.split("\n").reject { |it| /^\s*$/ =~ it }.map(&:strip).map { |expr|
31
+ block.call(expr, binding.eval(expr))
32
+ }.join("\n")
33
+ end
34
+
35
+ def p expr, &block
36
+ block ||= Formats.default
37
+ puts(expr){ |name, value| block.call name, value.inspect }
38
+ expr
39
+ end
40
+
41
+ def pp expr, &block
42
+ block ||= Formats.default
43
+ puts(expr){ |name, value| block.call name, value.pretty_inspect }
44
+ expr
45
+ end
46
+
47
+ def puts expr, &block
48
+ Kernel.puts debug expr, &block
49
+ end
50
+ end
51
+
52
+ module ProcWithBody
53
+ refine RubyVM::InstructionSequence do
54
+ def to_h
55
+ %i(
56
+ magic
57
+ major_version
58
+ minor_version
59
+ format_type
60
+ misc
61
+ label
62
+ path
63
+ absolute_path
64
+ first_lineno
65
+ type
66
+ locals
67
+ args
68
+ catch_table
69
+ bytecode
70
+ ).zip(to_a).to_h
71
+ end
72
+ end
73
+ using self
74
+
75
+ refine Proc do
76
+ def body
77
+ path, (start_lnum, start_col, end_lnum, end_col) = code_location
78
+
79
+ raise "Unsupported file" if path.nil? || path == "(irb)"
80
+
81
+ File.readlines(path).yield_self { |lines|
82
+ start_line, end_line = lines[start_lnum-1], lines[end_lnum-1]
83
+
84
+ # MEMO: Add support `-> { hoge }`
85
+ start_col += start_line[(start_col+1)..-1].yield_self { |line|
86
+ line =~ /^>\s*{/ ? line.index("{") + 1 # Support 2.5
87
+ : line =~ /^\s*{/ ? line.index("{") + 1 # Support 2.6 or later
88
+ : 0
89
+ }
90
+
91
+ if start_lnum == end_lnum
92
+ start_line[(start_col)...(end_col)]
93
+ elsif end_lnum - start_lnum == 1
94
+ start_line[(start_col)..-1] + end_line[0...(end_col)]
95
+ else
96
+ start_line[(start_col)..-1] +
97
+ lines[(start_lnum)...(end_lnum-1)].join +
98
+ end_line[0...(end_col)]
99
+ end.tap { |src|
100
+ break $1 if src =~ /(?:^do(.+)end$)/m
101
+ break $1 if src =~ /(?:^\s*{(.+)}$)/m
102
+ break ""
103
+ }
104
+ }
105
+ end
106
+
107
+ def code_location
108
+ RubyVM::InstructionSequence.of(self).to_h
109
+ .yield_self { |iseq| [iseq[:absolute_path], iseq.dig(:misc, :code_range) || iseq.dig(:misc, :code_location)] }
110
+ end
111
+ end
112
+ end
113
+
114
+ refine Kernel do
115
+ using ProcWithBody
116
+ using BindingDebug
117
+
118
+ def puts(*args, &block)
119
+ return super(*args) if block.nil?
120
+ binding.of_caller(1).puts block.body
121
+ end
122
+
123
+ def p(*args, &block)
124
+ return super(*args) if block.nil?
125
+ binding.of_caller(1).p block.body
126
+ end
127
+
128
+ def pp(*args, &block)
129
+ return super(*args) if block.nil?
130
+ binding.of_caller(1).pp block.body
131
+ end
132
+ end
21
133
  end
@@ -1,3 +1,3 @@
1
1
  module BindingDebug
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binding-debug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - manga_osyo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-24 00:00:00.000000000 Z
11
+ date: 2020-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: binding_of_caller
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - "~>"
31
+ - - ">="
18
32
  - !ruby/object:Gem::Version
19
- version: '1.16'
33
+ version: '0'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
- - - "~>"
38
+ - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: '1.16'
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - "~>"
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: '10.0'
47
+ version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '10.0'
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -59,6 +73,10 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
76
+ - ".github/workflows/macos.yml"
77
+ - ".github/workflows/ubuntu-rvm.yml"
78
+ - ".github/workflows/ubuntu.yml"
79
+ - ".github/workflows/windows.yml"
62
80
  - ".gitignore"
63
81
  - ".rspec"
64
82
  - ".travis.yml"
@@ -88,8 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
106
  - !ruby/object:Gem::Version
89
107
  version: '0'
90
108
  requirements: []
91
- rubyforge_project:
92
- rubygems_version: 2.7.6
109
+ rubygems_version: 3.1.2
93
110
  signing_key:
94
111
  specification_version: 4
95
112
  summary: 'Debug output. `name : value`'