Unverified Commit 5eef75b6 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #4034 from i110/i110/inspect-recursion

Let inspect recursion do the right thing
parents 9af3b7c6 d605b72c
......@@ -46,7 +46,9 @@ if Object.const_defined?(:Struct)
ary
end
def _inspect
def _inspect(recur_list)
return "#<struct #{self.class}:...>" if recur_list[self.object_id]
recur_list[self.object_id] = true
name = self.class.to_s
if name[0] == "#"
str = "#<struct "
......@@ -55,7 +57,7 @@ if Object.const_defined?(:Struct)
end
buf = []
self.each_pair do |k,v|
buf.push [k.to_s + "=" + v._inspect]
buf.push [k.to_s + "=" + v._inspect(recur_list)]
end
str + buf.join(", ") + ">"
end
......@@ -70,11 +72,7 @@ if Object.const_defined?(:Struct)
# 15.2.18.4.10(x)
#
def inspect
begin
self._inspect
rescue SystemStackError
"#<struct #{self.class.to_s}:...>"
end
self._inspect({})
end
##
......
......@@ -116,9 +116,10 @@ assert('struct dup') do
end
assert('struct inspect') do
c = Struct.new(:m1, :m2, :m3, :m4, :m5)
cc = c.new(1,2,3,4,5)
assert_equal "#<struct m1=1, m2=2, m3=3, m4=4, m5=5>", cc.inspect
c = Struct.new(:m1, :m2, :m3, :m4, :m5, :recur)
cc = c.new(1,2,3,4,5,nil)
cc.recur = cc
assert_equal "#<struct m1=1, m2=2, m3=3, m4=4, m5=5, recur=#<struct #{cc.class}:...>>", cc.inspect
end
assert('Struct#length, Struct#size') do
......
......@@ -83,13 +83,15 @@ class Array
self
end
def _inspect
def _inspect(recur_list)
size = self.size
return "[]" if size == 0
return "[...]" if recur_list[self.object_id]
recur_list[self.object_id] = true
ary=[]
i=0
while i<size
ary<<self[i].inspect
ary<<self[i]._inspect(recur_list)
i+=1
end
"["+ary.join(", ")+"]"
......@@ -99,11 +101,7 @@ class Array
#
# ISO 15.2.12.5.31 (x)
def inspect
begin
self._inspect
rescue SystemStackError
"[...]"
end
self._inspect({})
end
# ISO 15.2.12.5.32 (x)
alias to_s inspect
......
......@@ -186,8 +186,10 @@ class Hash
end
# internal method for Hash inspection
def _inspect
def _inspect(recur_list)
return "{}" if self.size == 0
return "{...}" if recur_list[self.object_id]
recur_list[self.object_id] = true
ary=[]
keys=self.keys
size=keys.size
......@@ -204,11 +206,7 @@ class Hash
#
# ISO 15.2.13.4.30 (x)
def inspect
begin
self._inspect
rescue SystemStackError
"{...}"
end
self._inspect({})
end
# ISO 15.2.13.4.31 (x)
alias to_s inspect
......
......@@ -40,7 +40,7 @@ module Kernel
end
# internal method for inspect
def _inspect
def _inspect(_recur_list)
self.inspect
end
......
......@@ -346,11 +346,12 @@ end
assert('Array#to_s', '15.2.12.5.31 / 15.2.12.5.32') do
a = [2, 3, 4, 5]
a[4] = a
r1 = a.to_s
r2 = a.inspect
assert_equal(r2, r1)
assert_equal("[2, 3, 4, 5]", r1)
assert_equal("[2, 3, 4, 5, [...]]", r1)
end
assert('Array#==', '15.2.12.5.33') do
......
......@@ -352,11 +352,13 @@ end
assert('Hash#inspect') do
h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }
h["recur"] = h
ret = h.to_s
assert_include ret, '"c"=>300'
assert_include ret, '"a"=>100'
assert_include ret, '"d"=>400'
assert_include ret, '"recur"=>{...}'
end
assert('Hash#rehash') do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment