P
parisnight
I frequently work on binary data files that contain data structures
with nested tagged unions: sf2, mp3, smf, CAN, J1939. I can parse the
files ad-hoc and look at data bytes to instantiate the correct final
object but it seems there should be a better classy way where the
parsing occurs hierarchically as the inheriting classes become more
specialized.
Say a file contains a collection of shapes. As you read bytes you
discover the shapes are squares and triangles, but you don't know that
until you've read some of the data that resides in the shape
superclass, so then you have to pass that data in when you instantiate
the new square object which is then used to assign to instance
variables of the superclass. It would be nicer to be able to
instantiate a generic superclass object, read its data, then based on
the values of the tags specialize the object to become a square or
triangular object and then the specialized object can read its data to
become more specific, and so on. Each child class could parse the bits
it knows about, and the object becomes more specialized as it reads
more.
In CLOS there are ways around this such as described in Peter Seibel's
book.
Does anyone know of any Ruby techniques that work well when reading
data containing tagged unions?
Thanks! Bob
with nested tagged unions: sf2, mp3, smf, CAN, J1939. I can parse the
files ad-hoc and look at data bytes to instantiate the correct final
object but it seems there should be a better classy way where the
parsing occurs hierarchically as the inheriting classes become more
specialized.
Say a file contains a collection of shapes. As you read bytes you
discover the shapes are squares and triangles, but you don't know that
until you've read some of the data that resides in the shape
superclass, so then you have to pass that data in when you instantiate
the new square object which is then used to assign to instance
variables of the superclass. It would be nicer to be able to
instantiate a generic superclass object, read its data, then based on
the values of the tags specialize the object to become a square or
triangular object and then the specialized object can read its data to
become more specific, and so on. Each child class could parse the bits
it knows about, and the object becomes more specialized as it reads
more.
In CLOS there are ways around this such as described in Peter Seibel's
book.
Does anyone know of any Ruby techniques that work well when reading
data containing tagged unions?
Thanks! Bob