Uninitialized stream (IOError)

So I’m getting an unexpected IOError in my code.

class ConfigStub < File
def initialize(location)
unless filename = location.gsub("/", “_”)
raise ArgumentError, INVALID_LOCATION_FORMAT
end
@stub = 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 = SanityChecker.new()
@config_stub = ConfigStub.new(location)
@directive = ["<Location #{location}>"]

    @config_stub.each do |line|
        @directive << line
    end
end # initialize

.
.
.
end # Directive class

When it goes to execute the @config_stub.each do |line| section I get
the
following error:

in `each': uninitialized stream (IOError)

My Ruby version is as follows:

ruby 1.9.1p129 (2009-05-12 revision 23412) [i686-linux]

What am I missing?

-Glen


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

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.

Thanks Brian.

On Thu, Jul 16, 2009 at 1:19 PM, Brian C. [email protected]
wrote:

@stub.read(*args)
If you want to subclass File, then you’ll need to ensure you call its
careful (as I think you’ve already discovered)

Regards,

Brian.

Posted via http://www.ruby-forum.com/.


“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs