# Inspecting cyclic structures

**URL:** <https://www.ruby-forum.com/t/inspecting-cyclic-structures/53683>\
**Category:** Ruby\
**Created:** [February 13, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/inspecting-cyclic-structures/53683 "2006-02-13T18:53:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![konsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@konsu](https://www.ruby-forum.com/u/konsu)\
**Post date:** [February 13, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/inspecting-cyclic-structures/53683/1 "2006-02-13T18:53:35Z")

</div>

hello,

is there a way to inspect an object only to a given depth to avoid  
infinite  
loops in case of referential loops? like irb does:

irb(main):001:0\> a = Object.new  
=\> #Object:0x2be6cd0  
irb(main):002:0\> b = Object.new  
=\> #Object:0x2be4900  
irb(main):003:0\> a.instance\_variable\_set(:@b, b)  
=\> #Object:0x2be4900  
irb(main):004:0\> b.instance\_variable\_set(:@a, a)  
=\> #\<Object:0x2be6cd0 @b=#\<Object:0x2be4900 @a=#\<Object:0x2be6cd0 …\>\>\>  
irb(main):005:0\> a.inspect  
=\> “#\<Object:0x2be6cd0 @b=#\<Object:0x2be4900 @a=#\<Object:0x2be6cd0  
…\>\>\>”  
irb(main):006:0\>

thanks  
konstantin

---

<div class="post-metadata">

**Author:** ![konsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@konsu](https://www.ruby-forum.com/u/konsu)\
**Post date:** [February 13, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/inspecting-cyclic-structures/53683/2 "2006-02-13T19:05:41Z")

</div>

On Tue, 14 Feb 2006, konsu wrote:

> =\> #Object:0x2be4900  
> irb(main):004:0\> b.instance\_variable\_set(:@a, a)  
> =\> #\<Object:0x2be6cd0 @b=#\<Object:0x2be4900 @a=#\<Object:0x2be6cd0 …\>\>\>  
> irb(main):005:0\> a.inspect  
> =\> “#\<Object:0x2be6cd0 @b=#\<Object:0x2be4900 @a=#\<Object:0x2be6cd0 …\>\>\>”  
> irb(main):006:0\>
> 
> thanks  
> konstantin

harp:~ \> cat a.rb  
a = Object.new  
b = Object.new  
a.instance\_variable\_set(:@b, b)  
b.instance\_variable\_set(:@a, a)

p a  
require “yaml” and y a  
require “pp” and pp a

harp:~ \> ruby a.rb  
#\<Object:0xb75cdc64 @b=#\<Object:0xb75cdc50 @a=#\<Object:0xb75cdc64  
…\>\>\>  
— &id001 !ruby/object  
b: !ruby/object  
a: \*id001  
#\<Object:0xb75cdc64 @b=#\<Object:0xb75cdc50 @a=#\<Object:0xb75cdc64  
…\>\>\>

hth.

-a

---

<div class="post-metadata">

**Author:** ![konsu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@konsu](https://www.ruby-forum.com/u/konsu)\
**Post date:** [February 13, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/inspecting-cyclic-structures/53683/3 "2006-02-13T19:08:31Z")

</div>

please disregard this, it seems to work outside irb too. in still  
outputs  
too much stuff in my case but it still does not loop forever.

konstantin

“konsu” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…
