From: Xeno Campanoli on 12 Feb 2010 16:50 I have an initialize method I want to run at the end of any daughter or granddaughter 'initialize' to make sure the state has been created properly, and I would rather specify the execution from the base class itself than count on those descendents to do it. xc -- "It's the preponderance, stupid!" - Professor Stephen Schneider, IPCC member
From: Ryan Davis on 12 Feb 2010 16:59 On Feb 12, 2010, at 13:50 , Xeno Campanoli wrote: > I have an initialize method I want to run at the end of any daughter or granddaughter 'initialize' to make sure the state has been created properly, and I would rather specify the execution from the base class itself than count on those descendents to do it. The most correct way to do it is to have all subclasses properly use super at the end of their initialize bodies: class Subclass < Superclass def initialize # ... stuff super end end There are "fancier" ways (read: overly clever), but doing it this way is cleaner, faster, and easier to debug / maintain.
From: Mat Brown on 12 Feb 2010 17:04 There's no simple way to do this. One possibility is to create a separate class method that is responsible for building the object, and then making the :new method private: class MyClass class <<self def build instance = new instance.some_attr = "some_value" end private :new end end Of course, subclasses could still override the build method, so you're not 100% safe. As far as I know, there's no way you could be in Ruby. Mat On Fri, Feb 12, 2010 at 16:50, Xeno Campanoli <xeno.campanoli(a)gmail.com> wrote: > I have an initialize method I want to run at the end of any daughter or > granddaughter 'initialize' to make sure the state has been created properly, > and I would rather specify the execution from the base class itself than > count on those descendents to do it. > > xc > > -- > "It's the preponderance, stupid!" - Professor Stephen Schneider, IPCC member > >
From: Xeno Campanoli on 12 Feb 2010 17:41 Ryan Davis wrote: > On Feb 12, 2010, at 13:50 , Xeno Campanoli wrote: > >> I have an initialize method I want to run at the end of any daughter or granddaughter 'initialize' to make sure the state has been created properly, and I would rather specify the execution from the base class itself than count on those descendents to do it. > > The most correct way to do it is to have all subclasses properly use super at the end of their initialize bodies: > > class Subclass < Superclass > def initialize > # ... stuff > super > end > end > > There are "fancier" ways (read: overly clever), but doing it this way is cleaner, faster, and easier to debug / maintain. > > > The problem with that is you may want some things from super available before you do other things in initialize. I figured out another way, which is to define initialize subroutines as pure virtual in the base class, called from initialize, then I put the thing at the end of initialize, and all the daughters only modify the called initializers, and all use the base class initialize method. There is nothing to force daughters not to make their own initialize, but at least this is nice and formal. -- "It's the preponderance, stupid!" - Professor Stephen Schneider, IPCC member
From: Marc Heiler on 12 Feb 2010 17:59 This actually reminds me of conditionally running hook-up methods. It would be interesting to see how much this meta-aspect could be included into ruby - instead of writing ruby code per se, write hooks upon hooks that get triggered when this or that changes. Hmm... sounds like another language though. -- Posted via http://www.ruby-forum.com/.
|
Next
|
Last
Pages: 1 2 Prev: can you help me about this deal? Next: What is the best exception to use for bad Data State? |