I am writing a reader for an embroidery machine file format in Ruby, which has two types of stitches:
- Regular stitches: Have a color and relative coordinates.
- Jump stitches: Also have a color and relative coordinates, but they are not visible (threads will be removed by hand in the final embroidery and are not visible in any previews that may be generated) and coordinate changes are usually much larger than for regular stitches
In Java, I would probably use an abstract base class "Stitch" and two classes "JumpStitch" and "RegularStitch" inheriting from it. I would then use either quick and dirty instanceof or overloaded methods (handle(JumpStitch stitch), handle(RegularStitch stitch) or something like that) to do something with those (I need the distinction between regular stitches and jump stitches).
How would you achieve something similar in Ruby (as Ruby lacks method overloading and instance_of? being a big nono according to https://stackoverflow.com/a/3893403/3818564)?
Two use cases incorporated from my comments below:
I have a list of all consecutive stitches in a List - some of them are of type RegularStitch, the other ones of type JumpStitch. Now I want to do something with this list. Say, draw a preview. I am iterating through the list, and decide what to do based on the type of the stitch: RegularStitch will be drawn with the given color, JumpStitch will not be drawn (but the coordinates will be updated from it as they are the basis for the following stitch). – no-trick-pony 22 mins ago
Another example: I want to actually operate an embroidery machine. I can operate the machine faster, when there are only regular stitches (of the same color), but have to slow down the machine if a JumpStitch is next. I hope that clarifies my intentions.