From: Hugh Sasse Staff Elec Eng Date: 2004-08-27T02:27:15+09:00 Subject: Re: Pretty Printing more widely On Fri, 27 Aug 2004, Gavin Sinclair wrote: > On Friday, August 27, 2004, 1:04:21 AM, Hugh wrote: > > I'm not arguing for reducing everything as far as possible. The point > is: [...] > If there were some value for "something", then 'p' and 'pp' would have > a certain consistency which I think they deserve. Oh, I see what you mean now. Thanks. > > 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 Blast! I should post with the editor open. To quote the first Perl book: "The Camels are coming!" -- the class name is PrettyPrint in pp.rb and prettyprint.rb, and the method defined for other classes is pretty_print, with no camels (which is so long as to cause me to type petty_print just then!) hence my mistake. > 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 that's a nice implementation. > > There's no need for specialisation, is there? > > [1,2,3].pp_s # -> "[1, 2, 3]\n" But then to get it out to a file I'd need to open("wossname.txt". "w"){|fp| fp.print [1,2,3].pp_s } which seems to me to be obscuring what I am trying to say with the code, as would . fp.print [1,2,3].pretty_print I'd rather use the concept of prettyprinting which has been around since at least 1982 (when I first bumped into it). I'd be thinking: File, prettyprint this rather tnan File, print this, prettily. Indeed, the prettily is an adverb and should (I think) be tightly coupled to the verb (method) rather than the object. Actually, it is really rather than thinking File, print (this, having prettified it). which feels more opaque to me. We say that ruby can be used as pseudocode, so I'd love to be able to use a simpler form, out of the box. > > Cheers, Gavin > Hugh > > >