From: Robert Klemme Date: 2005-10-27T20:42:04+09:00 Subject: Re: overriding #inspect Sean O'Halpin wrote: > On 10/26/05, Eric Mahurin wrote: >>> --- Sean O'Halpin wrote: >>> >>>> On 10/26/05, Eric Mahurin wrote: >>>>> Anybody know what the best way to override inspect where >>>> you >>>>> may have a recursive data structure? >>>> >>>> Check out Tanaka Akira's 'pp' lib (if only to convince >>>> yourself that >>>> there's no silver bullet!) >> >> Actually, as soon as I allowed myself to make an instance >> variable, it didn't seem so bad. Here's what I came up with: >> >> def inspect >> to_s[0..-2].concat(" #{ >> if defined?(@inspecting) >> "..." >> else >> begin >> @inspecting = true >> ... generate inspect contents here ... >> ensure >> remove_instance_variable(:@inspecting) >> end >> end >> }>") >> end > > But as soon as you start using flags to guard against cycles you have > to at least acknowledge that it won't be thread safe, unless you take > an approach like 'pp' (which stashes object_id in > Thread.current[InspectKey]). .... which reminds me of an RCR I've been wanting to submit for quite some time now: http://rcrchive.net/rcr/show/323 This could help here, too. Kind regards robert