From: Robert Klemme Date: 2007-02-05T07:35:08+09:00 Subject: Re: Inheriting from Fixnum On 04.02.2007 17:13, gga wrote: >> 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 ). Is it a year of the century or years difference? How would you implement to_days? I don't think this is worth another class - and btw. there are classes for date and time handling in Ruby which support all the math you typically need. Plus, you can also define methods in a more functional way (i.e. def year_to_months(year) 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). You can still use delegator... > 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. It's an internal optimization. Fixnums do not have to be created and they never have to be GC'ed. This goes a long way to make math faster than it would be with ordinary objects (at least for Fixnums). Regards robert