From: Logan Capaldo Date: 2007-02-05T12:52:26+09:00 Subject: Re: Inheriting from Fixnum On Mon, Feb 05, 2007 at 10:10:05AM +0900, gga wrote: > I can (and as I said, I have already), it is just pretty inelegant and > inefficient. Taking care of kind_of? and similar is pretty messy (but > needed as I do want to have Range of frames, for example). > Therefore, I want to know the reason of WHY I am forced to use a > delegator for deriving one of the most basic classes possible. So > far, I don't know why I need to go through such a cumbersome approach. > 1) Fixnum and Bignum are implementation details. If you want to inherit from anything it should be from Integer. Programmer's that add methods to just Fixnum are likely making an error. 2) There is no need to "take care of" kind_of? for Ranges. Ranges rely on #succ and #<=>, that's it. % irb irb(main):001:0> class Frame irb(main):002:1> def initialize( value ) irb(main):003:2> @value = value irb(main):004:2> end irb(main):005:1> irb(main):006:1* def succ irb(main):007:2> Frame.new( @value.succ ) irb(main):008:2> end irb(main):009:1> irb(main):010:1* def <=>(other) irb(main):011:2> @value <=> other.instance_variable_get("@value") irb(main):012:2> end irb(main):013:1> irb(main):014:1* include Comparable irb(main):015:1> end => Frame irb(main):016:0> start = Frame.new(0) => # irb(main):017:0> stop = Frame.new(5) => # irb(main):018:0> (start..stop).to_a => [#, #, #, #, #, #] irb(main):019:0> (start..stop).each { |x| p x } # # # # # # => #..# irb(main):020:0> (start..stop).include? Frame.new(3) => true irb(main):021:0> (start..stop).include? Frame.new(9) => false 3) The reason you are forced to use a delegator to "derive" from one of the most basic classes possible is because it is one of the most basic classes possible. A Fixnum is a primitive. Inheriting from it is akin to trying to implement arrays in C w/o using arrays. (Note that saying "well I can do it in python" doesn't hold water, as python selected different primitives)