uninitialized stream (IOError)

Discussion in 'Ruby' started by Glen Holcomb, Jul 16, 2009.

  1. Glen Holcomb

    Glen Holcomb Guest

    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
    Glen Holcomb, Jul 16, 2009
    #1
    1. Advertising

  2. 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/.
    Brian Candler, Jul 16, 2009
    #2
    1. Advertising

  3. Glen Holcomb

    Glen Holcomb Guest

    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)
    Glen Holcomb, Jul 16, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Cobban

    Xerces parse aborted on IOError

    Jim Cobban, Dec 5, 2003, in forum: XML
    Replies:
    0
    Views:
    395
    Jim Cobban
    Dec 5, 2003
  2. Bart Nessux

    urllib IOError Exception

    Bart Nessux, Jun 11, 2004, in forum: Python
    Replies:
    4
    Views:
    1,536
    John J. Lee
    Jun 14, 2004
  3. Jay Donnell

    IOError: [Errno 32] Broken pipe

    Jay Donnell, Jun 28, 2004, in forum: Python
    Replies:
    2
    Views:
    8,390
    Donn Cave
    Jun 29, 2004
  4. daz
    Replies:
    1
    Views:
    132
  5. Eric Will

    stream closed (IOError)

    Eric Will, Oct 5, 2008, in forum: Ruby
    Replies:
    9
    Views:
    250
    Eric Will
    Oct 5, 2008
Loading...

Share This Page