So here I am working in Ruby again....
The object system works fine for the things I'd wanted to do up to this point. And, unlike the other object systems I'd encountered, it actually makes sense to me.
But now there's an interesting situation, which surely isn't all that uncommon:
I'm presented with an array of bytes, purported to be a received communication packet.
A few of the bytes have constant meanings: the outermost wrapper.
Within that wrapper are more bytes, which have various meanings depending on the value of the format byte in the outer wrapper. Typically, this will be another wrapper around an actual payload-data field.
The obvious thing to do with this is to have a high-level class that has the basics (information represented by the outer wrapper), and various subclasses to represent the different formats.
So... what I intuitively want to do is to pass the input array to the constructor for the most abstract class, and have it morph into the appropriate subclass based on the format byte, chaining the constructor for the subclass.
Alas, this is exactly backwards to the way instantiation works in Ruby. So far as I've found so far, I need to identify the subclass in advance, and instantiate that, leading to the invocation of the superclass constructor.
Hm. Well... I guess what I could do is to add a class method to the top-level class, so the user would call, say, FiddlyMessage.from_buffer(input_buffer), and that would find the format byte in the buffer and invoke the appropriate FiddlyMessageSubclass.new(input_buffer) to get the actual work done.