From: Ara.T.Howard@... Date: 2004-10-12T23:54:36+09:00 Subject: return val of initialize [WAS] Re: tempfiles created by tmpfile removed at exit - can I change this easily? On Tue, 12 Oct 2004, Robert Klemme wrote: >> require 'tempfile' >> >> class Tmpfile < Tempfile >> class << self; attr :litter, true; end >> def initialize(*a,&b) >> t = super >> ObjectSpace::undefine_finalizer self if self.class.litter >> t >> end >> end > > What do you need that "t" for? IMHO it's completely superfluous. i always use a clearly defined return value in my code as, at minimum, doccumentation. when i glance at the above it's instantly clear to me (eg. when re-reading this in six months) that i am doing nothing except removing the finalizer from the instance and returning it. in general i find that the magic ruby rule of returning value of the last statement bad to rely upon in all but the most simply cases - but for the real reason see below: > The return value of initialize is always completely ignored. that cannot be asserted: harp:~ > cat a.rb class A def initialize 42 end def reinit p initialize end end A::new.reinit harp:~ > ruby a.rb 42 if you mean to say that 'the value of initialize is ignored by default for the single case when it is called by the default Object#new method' then you are correct. remember, initialize is simply a method called, in one single case, to fill in blank objects - it's quite possible and acceptable to call it with side effects from within the class, or from within subclasses. in general i think it's poor practice to leave a method with a random return value: one should return something meaningful and, if that's not possible, return self to allow chaining. > Also from an aesthetical point of view, I'd expect the flag "litter" to > behave exactly the other way round: if litter is true throw it away; you > keep it if litter is true. tempfiles are thrown away 'properly' in the normal case - eg. they do not 'litter' /tmp (a very dangerous practice). i would consider a program that did not clean up after itself to have 'littered' - but it's of no matter: change it if you like. cheers. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================