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. Advertisements

  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. Advertisements

  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. Advertisements

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. Rasmusson, Lars
    Replies:
    1
    Views:
    928
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    878
    Alex Buell
    Apr 27, 2006
  3. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    609
    John Harrison
    Feb 17, 2007
  4. dolphin
    Replies:
    6
    Views:
    740
    Thomas Fritsch
    Mar 18, 2007
  5. mrstephengross
    Replies:
    3
    Views:
    554
    James Kanze
    May 10, 2007
  6. Kashif Ur Rehman
    Replies:
    2
    Views:
    1,022
    Tom Hawtin
    May 17, 2007
  7. daz
    Replies:
    1
    Views:
    361
  8. Eric Will

    stream closed (IOError)

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