From: Sean O'Halpin Date: 2005-10-27T19:49:29+09:00 Subject: Re: overriding #inspect 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]). Regards, Sean