From: gga Date: 2007-02-05T01:15:09+09:00 Subject: Re: Inheriting from Fixnum > > I don't believe you can use Numeric.coerce since it does not know > anything about your class. All the math etc. will be broken. > According to the docs of Numeric.coerce, it says it returns the original base class. If the method had been coded using a form like: self.class.new() it would work properly across any numeric and derived type (assuming of course new existed in the first place). Looking at the C source, I can see of course this is not case, so I need to provide my own coerce, but oh well... the docs description are somewhat incorrect. > I try differently: what kind of class do you want to implement? Why do > you think you need to inherit? What capabilities should instances of > your class have? > Simple. I want a Fixnum. I don't want to pollute Fixnum's namespace and I don't want a Fixnum to automatically be promoted to my class, while at the same time, my class should be compatible with ALL of fixnum methods (odd? even?, etc). Think of a Year class (yes, I know there's Date and DateTime -- my class is not exactly this but will do as an example). A year is represented by a Fixnum, but it is not a Fixnum. Yet I expect all Fixnum operations to work on it, returning a new Year in case of additions, etc. I also don't want a normal Fixnum to be treated as a year without a conversion function or instantiation thru Year.new(). Also, I want to have a lof of conversion functions within Year that are not applicable to Fixnum ( to_months, to_days, etc ). Right now, to do that, I have to actually recreate all of the methods in Fixnum/Numeric/etc. while if I was inheriting from it, I would only have to recreate half of them or less or rely on method_missing (yuck). Looking at the C code, I see that new() exists in Numeric and Float, but it is specifically removed: rb_cInteger = rb_define_class("Integer", rb_cNumeric); rb_undef_alloc_func(rb_cInteger); rb_undef_method(CLASS_OF(rb_cInteger), "new"); #....and for floats.. rb_undef_alloc_func(rb_cFloat); rb_undef_method(CLASS_OF(rb_cFloat), "new"); The ruby source code contains no explanation for the removal of new. That's why I'm wondering what the reasoning behind this was. It does not seem to be technical limitation in the ruby interpreter. Currently, it does not make much sense to me.