Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > uninitialized stream (IOError)

Reply
Thread Tools

uninitialized stream (IOError)

 
 
Glen Holcomb
Guest
Posts: n/a
 
      07-16-2009
So I'm getting an unexpected IOError in my code.

class ConfigStub < File
def initialize(location)
unless filename =3D location.gsub("/", "_")
raise ArgumentError, INVALID_LOCATION_FORMAT
end
@stub =3D File.open(CONFIG_PATH + filename, "w+") # This might need=
to
be more sophisticated depending on the rule
end # initialize
end # ConfigStub class

class Directive
def initialize(location)
@sanity_checker =3D SanityChecker.new()
@config_stub =3D ConfigStub.new(location)
@directive =3D ["<Location #{location}>"]

@config_stub.each do |line|
@directive << line
end
end # initialize
 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      07-16-2009
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 = 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/.

 
Reply With Quote
 
 
 
 
Glen Holcomb
Guest
Posts: n/a
 
      07-16-2009
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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert DVD with subtitle stream to DivX with same subtitle stream(selectable) malise Software 2 04-17-2007 09:15 AM
what is the different between byte stream and character stream? dolphin Java 6 03-18-2007 01:58 PM
get stream mode flags from an opened stream Alexander Korsunsky C++ 1 02-17-2007 10:38 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Doing readline in a thread from a popen4('rsync ...') stream blocks when the stream ends. Rasmusson, Lars Python 1 04-30-2004 08:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57