From: Mathieu Bouchard Date: 2002-01-24T00:24:18+09:00 Subject: Re: Newbie: simple data-type problem On Wed, 23 Jan 2002, Dave Carmean wrote: > What I though I'd try is to create a class which inherits from > Fixnum, and do range checking in the initializer. Doesn't work. > Can't figure out how to initialize it, because Fixnum doesn't have > a new() method. Classes are there to add functionality, not remove it. You want to describe a subset of the Fixnum class in which all methods are the same. I have worked on a concept of "type" separate from "class" to deal with this kind of problem. A type only defines a === method that tells whether the given object is in the type or not. class IntType def new(range) @range = range end def ===(obj) Integer === obj && @range === obj end end Uint16 = IntType.new(0...2**16) case x when Uint16 # ok else raise TypeError, "#{x} is not a Uint16" end > my new class already has so many built-in methods, stuff that > I'd want sent to the contained Fixnum gets intercepted. Do you want to manipulate integer values, or integer variables ? What does this class do apart from being a Fixnum or a holder thereof? ________________________________________________________________ Mathieu Bouchard http://hostname.2y.net/~matju