You want to create a class that acts like a subclass of
Float, or one of Ruby's other built-in
numeric classes. This wondrous class can be used in arithmetic along
Float objects, and it will usually act like
one of those objects, but it will have a different representation or
implement extra functionality.
Let's take a concrete example and consider the possibilities.
Suppose you wanted to create a class that acts just like
Integer, except its string representation is
a hexadecimal string beginning with "0x". Where a
Fixnum's string representation might be
"208", this class would represent 208 as "0xc8".
You could modify
to output a hexadecimal string. This would probably drive you insane
because it would change the behavior for all
Integer objects. From that point
on, nearly all the numbers you use would have hexadecimal string
representations. You probably want hexadecimal string representations
only for a few of your numbers.
This is a job for a subclass, but you can't usefully subclass
Fixnum (the Discussion explains why
this is so). The only alternative is delegation. You need to create a
class that contains an instance of
Fixnum, and almost always delegates method
calls to that instance. The only method calls it doesn't delegate
should be the ones that it wants to override.
The simplest way to do this is to create a custom delegator
class with the
delegate library. A class created with ...