From: Glen Holcomb Date: 2009-07-17T06:14:41+09:00 Subject: Re: uninitialized stream (IOError) --001636c5abdfcd6dad046ed92843 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable Thanks Brian. On Thu, Jul 16, 2009 at 1:19 PM, Brian Candler wrote: > You're trying to mix subclassing and delegation. > > That is: your object is a subclass of File, and therefore an instance of > ConfigStub *is* a File. However you are also opening another File and > storing it in the instance variable @stub, and therefore it *has* a File > as well. > > Choose one or the other. In my experience, delegation is the most > flexible and understandable approach, and although it needs some setting > up it ends up being simplest in the long run. > > You can delegate manually: > > class ConfigStub > def initialize(location) > @stub =3D File.open(...) > end > def each(*args,&blk) > @stub.each(*args,&blk) > end > def read(*args) > @stub.read(*args) > end > ... etc, and/or > def method_missing(*args,&blk) > @stub.__send__(*args,&blk) > end > end > > or use a wrapped up version of this - check out SimpleDelegator in > delegate.rb in the standard library. > > If you want to subclass File, then you'll need to ensure you call its > initialize method. Probably something like this (untested): > > class ConfigStub < File > def initialize(location) > .. > super(CONFIG_PATH + filename, "w+") # in the superclass > end > end > > But subclassing core classes can get you tied up in knots if you're not > careful (as I think you've already discovered) > > Regards, > > Brian. > -- > Posted via http://www.ruby-forum.com/. > > --=20 "Hey brother Christian with your high and mighty errand, Your actions speak so loud, I can=92t hear a word you=92re saying." -Greg Graffin (Bad Religion) --001636c5abdfcd6dad046ed92843--