From: Robert Klemme Date: 2010-08-18T20:27:06+09:00 Subject: Re: Making File.open work on gzipped files 2010/8/18 Martin Hansen : > Thanks Brian and Robert. The below snippet appears to be working nicely > - though I am not sure that the file is closed if zipped? > > > > class Parser >  def self.open(*args) >    ios = File.open(*args) > >    begin >      ios = Zlib::GzipReader.new(ios) >    rescue >      ios.rewind >    end > >    parse = self.new(ios) > >    if block_given? >      begin >        yield parse >      ensure >        ios.close >      end > >      return true >    else >      return parse >    end >  end > end I would apply these changes: 1. refactor opening code (everything before "if block_given?") into a separate method which returns either IO or GzipReader. 2. fold parse and ios into one (i.e. the value returned from the other method). See http://ruby-doc.org/core/classes/Zlib/GzipFile.html#method-M007448 3. In case of block_given? do not return true but rather nothing (i.e. what the block returned). This is more flexible. That way your code will become simpler. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/