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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd79e6aeb0a29ecf4dbf971839a6f4fb93263bf6
4
- data.tar.gz: f9d7486ca23a0d10023d8571935083a41d475e28
3
+ metadata.gz: 95a0b85b80910af7d15324eb9397d7df64967137
4
+ data.tar.gz: 93182c01723a4cb3a6c6e61383613ac77a3c46b5
5
5
  SHA512:
6
- metadata.gz: f8d48dc90b64d79641f36e4bfe60e72f2549c6fdb22544b4654e4609f17fea57093a2b0de0a86334eac5c4ffed33cdea950525a74727f87c1aa818777163eba5
7
- data.tar.gz: 1d14a942a7bc65dd386cb5df3eaf520cf9c7e894dd1ae02df92a757edd5d37d8213a2b8bb990c30d6392a09764ae9983b680bec8a94e970209e125c770d41a7a
6
+ metadata.gz: 9c314098e50af28b1dbb4cb32f0d4493c103875a085315dab02f8b982ebe854d89f628de34bb5e791cacb8d26840f2d8151390972c57b4dd1ad836622c4c79fc
7
+ data.tar.gz: e39bd908b173380d7023ebe8313ec61670529741818e76beeb771c39d184627705cbf625e9a02c578d1058ccb83591171834655acb824d53e813b13671cbfd66
@@ -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
- _to_str(object, 0)
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
- type = obj.class.name
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
- if obj.kind_of?(Array)
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
- elsif obj.kind_of?(Hash)
29
- elsif obj.kind_of?(String)
30
- elsif obj.respond_to?(:instance_variables)
31
- obj = obj.instance_variables.inject({}) {|h,key|
32
- key_str = key.to_s.sub("@", "")
33
- h[key_str] = obj.instance_variable_get(key)
34
- h
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
- if obj.kind_of?(Hash)
39
- str = "#{type} Object\n"
40
- str += tab1 + "(\n"
41
- obj.each_pair do |key,value|
42
- str += sprintf("%s[%s] => %s\n", tab2, key, _to_str(value, indent+2))
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.to_s
88
+ str = "#{obj}"
47
89
  end
48
90
 
49
91
  str
@@ -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(/^(?:Array|.*? Object)\n#{tab}\(\n(.*)#{tab}\)$/m)
20
- if md
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|
@@ -0,0 +1,14 @@
1
+ module PrintR
2
+ class Recursion < RuntimeError
3
+ attr_reader :object_type
4
+
5
+ def initialize(error_message=nil)
6
+ @object_type = error_message
7
+ super(error_message)
8
+ end
9
+
10
+ def to_s
11
+ "#{object_type} *RECURSION*"
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module PrintR
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
data/lib/print_r.rb CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'print_r/parser'
5
5
  require 'print_r/generator'
6
+ require 'print_r/recursion'
6
7
  require 'print_r/version'
7
8
 
8
9
  module PrintR
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.3.0
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-04-16 00:00:00.000000000 Z
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