G
gga
Now... this should be simple, but, alas, it is not.
I'm creating some special numeric classes, and I wanted to derive from
Fixnum, for example.
class Frame < Fixnum
end
While the above code works, doing then:
Frame.new(20)
will fail. Technically, it is true that when creating a class derived
from a primitive type, most
operators (methods) will need to be recreated, but this should still
be possible. It isn't clear to me
why this is not allowed.
Currently, I've gone and created a new class that contains a Fixnum
and redefined all the needed
methods. That is:
class Frame
def initialize(x)
@x = Integer(x)
end
def to_frame
self
end
def +(x)
Frame.new( self + x.to_frame )
end
# many more methods here...
def method_missing(m, *args)
@x.send(m, *args)
end
end
Unfortunately, since the class does not derive from Fixnum properly,
queries such as
kind_of?() or responds_to?() do not quite do what they are supposed
to.
Can anyone comment on why this design was chosen (or how can this
better be done)?
I'm creating some special numeric classes, and I wanted to derive from
Fixnum, for example.
class Frame < Fixnum
end
While the above code works, doing then:
Frame.new(20)
will fail. Technically, it is true that when creating a class derived
from a primitive type, most
operators (methods) will need to be recreated, but this should still
be possible. It isn't clear to me
why this is not allowed.
Currently, I've gone and created a new class that contains a Fixnum
and redefined all the needed
methods. That is:
class Frame
def initialize(x)
@x = Integer(x)
end
def to_frame
self
end
def +(x)
Frame.new( self + x.to_frame )
end
# many more methods here...
def method_missing(m, *args)
@x.send(m, *args)
end
end
Unfortunately, since the class does not derive from Fixnum properly,
queries such as
kind_of?() or responds_to?() do not quite do what they are supposed
to.
Can anyone comment on why this design was chosen (or how can this
better be done)?