print_r 1.3.0 → 1.4.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 +4 -4
- data/lib/print_r/generator.rb +59 -17
- data/lib/print_r/parser.rb +3 -2
- data/lib/print_r/recursion.rb +14 -0
- data/lib/print_r/version.rb +1 -1
- data/lib/print_r.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95a0b85b80910af7d15324eb9397d7df64967137
|
4
|
+
data.tar.gz: 93182c01723a4cb3a6c6e61383613ac77a3c46b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c314098e50af28b1dbb4cb32f0d4493c103875a085315dab02f8b982ebe854d89f628de34bb5e791cacb8d26840f2d8151390972c57b4dd1ad836622c4c79fc
|
7
|
+
data.tar.gz: e39bd908b173380d7023ebe8313ec61670529741818e76beeb771c39d184627705cbf625e9a02c578d1058ccb83591171834655acb824d53e813b13671cbfd66
|
data/lib/print_r/generator.rb
CHANGED
@@ -7,43 +7,85 @@ module PrintR
|
|
7
7
|
|
8
8
|
def initialize(object)
|
9
9
|
@object = object
|
10
|
+
@recursive_objects = nil
|
10
11
|
end
|
11
12
|
|
12
13
|
def generate
|
13
|
-
|
14
|
+
begin
|
15
|
+
@recursive_objects = Hash.new(0)
|
16
|
+
_to_str(object, 0)
|
17
|
+
ensure
|
18
|
+
@recursive_objects = nil
|
19
|
+
end
|
14
20
|
end
|
15
21
|
|
16
22
|
def _to_str(obj, indent)
|
17
23
|
tab1 = " " * indent
|
18
24
|
tab2 = " " * (indent+1)
|
19
25
|
str = nil
|
20
|
-
|
26
|
+
object_type = obj.class.name
|
27
|
+
|
28
|
+
is_recursive = 1<@recursive_objects[obj.object_id]
|
29
|
+
@recursive_objects[obj.object_id] += 1
|
21
30
|
|
22
|
-
|
31
|
+
case obj
|
32
|
+
when Array
|
33
|
+
# hash of values with index
|
23
34
|
obj2 = {}
|
24
35
|
obj.each_with_index do |value,i|
|
25
36
|
obj2[i] = value
|
26
37
|
end
|
27
38
|
obj = obj2
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
39
|
+
when Hash
|
40
|
+
# do nothing
|
41
|
+
when TrueClass
|
42
|
+
# fixnum 1
|
43
|
+
obj = 1
|
44
|
+
when FalseClass
|
45
|
+
# fixnum 0
|
46
|
+
# not compatible with print_r() of PHP
|
47
|
+
obj = 0
|
48
|
+
when NilClass
|
49
|
+
# empty string
|
50
|
+
obj = ""
|
51
|
+
when Exception
|
52
|
+
# to hash
|
53
|
+
obj = obj.instance_eval{
|
54
|
+
{
|
55
|
+
:message => self.message,
|
56
|
+
:backtrace => self.backtrace,
|
57
|
+
}
|
35
58
|
}
|
59
|
+
else
|
60
|
+
if obj.respond_to?(:instance_variables) && 0<obj.instance_variables.length
|
61
|
+
# instance_variables
|
62
|
+
obj = obj.instance_variables.inject({}) {|h,key|
|
63
|
+
key_str = key.to_s.sub("@", "")
|
64
|
+
h[key_str] = obj.instance_variable_get(key)
|
65
|
+
h
|
66
|
+
}
|
67
|
+
elsif obj.respond_to?(:to_s)
|
68
|
+
# to_s
|
69
|
+
obj = obj.to_s
|
70
|
+
end
|
36
71
|
end
|
37
72
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
73
|
+
# to string
|
74
|
+
case obj
|
75
|
+
when Hash
|
76
|
+
str = "#{object_type} Object\n"
|
77
|
+
|
78
|
+
if is_recursive
|
79
|
+
str += " *RECURSION*"
|
80
|
+
else
|
81
|
+
str += tab1 + "(\n"
|
82
|
+
obj.each_pair do |key,value|
|
83
|
+
str += sprintf("%s[%s] => %s\n", tab2, key, _to_str(value, indent+2))
|
84
|
+
end
|
85
|
+
str += tab1 + ")\n"
|
43
86
|
end
|
44
|
-
str += tab1 + ")\n"
|
45
87
|
else
|
46
|
-
str = obj
|
88
|
+
str = "#{obj}"
|
47
89
|
end
|
48
90
|
|
49
91
|
str
|
data/lib/print_r/parser.rb
CHANGED
@@ -16,8 +16,9 @@ module PrintR
|
|
16
16
|
def _to_object(str, indent)
|
17
17
|
str = str.strip
|
18
18
|
tab = " " * indent
|
19
|
-
md = str.match(/^(
|
20
|
-
|
19
|
+
if md = str.match(/^(Array|[^ ]*? Object)\n \*RECURSION\*$/m)
|
20
|
+
obj = Recursion.new(md[1])
|
21
|
+
elsif md = str.match(/^(?:Array|[^ ]*? Object)\n#{tab}\(\n(.*)#{tab}\)$/m)
|
21
22
|
obj = _to_key_values(md[1].split(/(\r\n|\r|\n)/), indent+1)
|
22
23
|
|
23
24
|
obj.each do |key, value|
|
data/lib/print_r/version.rb
CHANGED
data/lib/print_r.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: print_r
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshida Tetsuya
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This is a print_r() implementation.
|
14
14
|
email:
|
@@ -24,6 +24,7 @@ files:
|
|
24
24
|
- lib/print_r/core_ext.rb
|
25
25
|
- lib/print_r/generator.rb
|
26
26
|
- lib/print_r/parser.rb
|
27
|
+
- lib/print_r/recursion.rb
|
27
28
|
- lib/print_r/version.rb
|
28
29
|
- print_r.gemspec
|
29
30
|
homepage: https://github.com/yoshida-eth0/ruby-print_r
|