duck_puncher 2.2.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/bin/console +1 -1
- data/lib/duck_puncher/method.rb +12 -0
- data/lib/duck_puncher/version.rb +1 -1
- data/test/duck_puncher/method_test.rb +8 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee22023f5904a77f13fdc09ac72eefa9170938ea
|
4
|
+
data.tar.gz: 28fb5cc8000d7de6240b6cf6c41e476861c4e255
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb0141394a399c457e4650f607ed66c7c924aa9c7e15030faafd1c20a18d812b620bc13864bcda8904720aa1218f65d915fdf8601af4e6dc4c412ebb722a0523
|
7
|
+
data.tar.gz: 9f86ea17b513e5e3e0e4da59c6665c398e6f26b8229b9d58f5f012578147577ad7755ef1775626f75ab69063159abf45b00acb03c323ff583905c52c2fbe98ec
|
data/README.md
CHANGED
@@ -27,6 +27,7 @@ These are the ducks I can punch:
|
|
27
27
|
>> Pry.start
|
28
28
|
[1] pry(main)>
|
29
29
|
Method#to_instruct => `Benchmark.method(:measure).to_instruct` returns the Ruby VM instruction sequence for the method
|
30
|
+
Method#to_source => `Benchmark.method(:measure).to_source` returns the method definition as a string
|
30
31
|
|
31
32
|
|
32
33
|
## Install
|
data/bin/console
CHANGED
data/lib/duck_puncher/method.rb
CHANGED
@@ -5,6 +5,10 @@ module DuckPuncher
|
|
5
5
|
RubyVM::InstructionSequence.new(definition.lines.join).disasm if definition.lines.any?
|
6
6
|
end
|
7
7
|
|
8
|
+
def to_source
|
9
|
+
Definition.new(self).to_s
|
10
|
+
end
|
11
|
+
|
8
12
|
class Definition
|
9
13
|
def initialize(method_handle)
|
10
14
|
@file_path, @line_num = *method_handle.source_location
|
@@ -34,6 +38,14 @@ module DuckPuncher
|
|
34
38
|
@lines
|
35
39
|
end
|
36
40
|
|
41
|
+
def to_s
|
42
|
+
if lines.any?
|
43
|
+
lines.join.gsub /^\s{#{find_indent_size(lines.first)}}/, ''
|
44
|
+
else
|
45
|
+
''
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
37
49
|
def find_indent_size(line)
|
38
50
|
line[/(\s*)/].size
|
39
51
|
end
|
data/lib/duck_puncher/version.rb
CHANGED
@@ -9,13 +9,6 @@ class MethodTest < MiniTest::Test
|
|
9
9
|
@subject = Wut.new
|
10
10
|
end
|
11
11
|
|
12
|
-
# Called after every test method runs. Can be used to tear
|
13
|
-
# down fixture information.
|
14
|
-
|
15
|
-
def teardown
|
16
|
-
# Do nothing
|
17
|
-
end
|
18
|
-
|
19
12
|
def test_to_instruct
|
20
13
|
assert_match /RubyVM::InstructionSequence:to_a/, @subject.method(:to_a).to_instruct
|
21
14
|
assert_match /newarray/, @subject.method(:to_a).to_instruct
|
@@ -27,4 +20,12 @@ class MethodTest < MiniTest::Test
|
|
27
20
|
assert_match /RubyVM::InstructionSequence:to_f/, @subject.method(:to_f).to_instruct
|
28
21
|
assert_match /getconstant\s*:INFINITY/, @subject.method(:to_f).to_instruct
|
29
22
|
end
|
23
|
+
|
24
|
+
def test_to_source
|
25
|
+
assert_equal "def to_a\n ['w'] + ['u'] + ['t']\nend\n", @subject.method(:to_a).to_source
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_to_source_with_no_source
|
29
|
+
assert_equal '', @subject.method(:object_id).to_source
|
30
|
+
end
|
30
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: duck_puncher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Buckley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|