From: Gavin Sinclair Date: 2004-08-27T01:19:05+09:00 Subject: Re: Pretty Printing more widely On Friday, August 27, 2004, 1:04:21 AM, Hugh wrote: >> I agree in spirit, but let me offer an alternative. #pp is >> conceptually similar to #p. #p doesn't exist in IO-like classes >> because it's simply a shortcut for 'puts X.inspect'. To keep the > I don't agree with that reason. puts is just a shortcut for adding > "\n" into print statements, but we still have it. I'm not arguing for reducing everything as far as possible. The point is: x.inspect returns a string and 'p x' prints that string. x.something returns a string and 'pp x' prints that string. If there were some value for "something", then 'p' and 'pp' would have a certain consistency which I think they deserve. For the record, I would't mind #p and #pp to be defined in IO-like objects, but I'm not clamouring for it. >> analogy between #p and #pp, why can't #pp be a shortcut for "puts >> X.something"? > It is -- it is x.prettyprint. The problem is that it goes to $>, > which you may not want to meddle with. The following gives me a NoMethodError. require 'pp' [1,2,3].prettyprint And you're right, I have no interest in meddling with $> :) >> In 'extensions', I define Object#pp_s. It's not a great name, but > define it as what? When IO inherits it the meaning will surely > neet to be different from when Array inherits it? class Object def pp_s pps = StringIO.new PP.pp(self, pps) pps.string end end There's no need for specialisation, is there? [1,2,3].pp_s # -> "[1, 2, 3]\n" Cheers, Gavin