inspecting cyclic structures

K

konsu

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
 
K

konsu

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
 
A

ara.t.howard

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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top