On 03.02.2007 09:07, gga wrote:
> 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:

> 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.

Note that you generally should not use #kind_of? and like methods (-> 
Duck Typing).

> Can anyone comment on why this design was chosen (or how can this
> better be done)?

Inheriting from Fixnum is not a a good idea since instances cannot be 
constructed as Wolfgang pointed out.  The way to go is delegation, as 
you did.  You can however inherit any of the other classes in the 
inheritance chain of Fixnum:

irb(main):006:0> 1.class
=> Fixnum
irb(main):007:0> 1.class.ancestors
=> [Fixnum, Integer, Precision, Numeric, Comparable, Object, Kernel]

One of Integer, Precision or Numeric seems most appropriate.

Few hints: there is "delegate" which greatly simplifies this task.

http://ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html

Also, if you want to do math you should implement #coerce.  Note that 
unary operators have an at appended:

irb(main):016:0> class Foo; def -@; "minus" end end
=> nil
irb(main):017:0> -Foo.new
=> "minus"

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/98763

Kind regards

	robert