Allow creating Kaitai::Struct::Stream from StringIO (not just String)
generalmimon opened this issue · comments
Petr Pučil commented
This code:
require 'stringio'
require_relative 'lib/kaitai/struct/struct'
io = Kaitai::Struct::Stream.new(StringIO.new("PK\x03\x04"))
currently fails with
$ ruby t.rb
C:/temp/kaitai_struct/runtime/ruby/lib/kaitai/struct/struct.rb:104:in `initialize': can be initialized with IO, SubIO or String only (TypeError)
from t.rb:4:in `new'
from t.rb:4:in `<main>'
because the Stream.new
implementation expects to receive a plain String
instead:
io = Kaitai::Struct::Stream.new("PK\x03\x04")
and it will create a StringIO
itself:
kaitai_struct_ruby_runtime/lib/kaitai/struct/struct.rb
Lines 97 to 104 in 515d34d
I think rejecting the accepted StringIO
and insisting on creating it "automatically" is an unnecessary restriction, and initialization with StringIO
should be allowed as well to make it foolproof.
Mikhail Yakshin commented
Interesting... I've never realized that StringIO is not derived from IO in Ruby. I've tried to fix it.