Can't read from GzipReader: undefined method `external_encoding' #105

Open
ghostganz opened this Issue Nov 25, 2012 · 3 comments

3 participants

@ghostganz

I was doing this successfully with Ruby 1.9.2 and Syck:

require 'zlib'
require 'yaml'
data = YAML.load(Zlib::GzipReader.new(open('foo.yml.gz'))

but with Psych it fails like this:

NoMethodError: undefined method `external_encoding' for #<Zlib::GzipReader:0x007fa36303ffa8>

so it looks like it's assuming things about the input stream that aren't always true.

@envygeeks

@ghostganz Because you sent it the Zlib::GzipReader object and didn't read the file to a string.

require 'zlib'
require 'yaml'
data = YAML.load(Zlib::GzipReader.new(open('foo.yml.gz')).read)
@ghostganz

But I don't want to read it to a string, that was the point. Working with input streams directly used to work with Syck, but now it doesn't with Psych.

@akostadinov

any update? why not allow reading yaml from a stream? This is most natural and memory efficient. After all it can read from a file stream, why not from a zstream?

hey, monkey patch workaround for the rescue:

    def load_yaml_from_gz(gz)
      loaded = nil
      Zlib::GzipReader.open(gz) do |f|
        class << f
          def external_encoding
            Encoding::UTF_8
          end
        end
        loaded = YAML.load(f, gz)
      end
      return loaded
    end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment