From: Christopher Dicely Date: 2008-04-23T05:07:36+09:00 Subject: Re: A couple of questions regarding class design On Mon, Apr 21, 2008 at 11:35 AM, Michael W. Ryder <_mwryder@worldnet.att.net> wrote: > Joel VanderWerf wrote: > > > Michael W. Ryder wrote: > > > > > I am part way through implementing a Rational math class to further my > understanding of Ruby and had a couple of questions that I can't find the > answer to in Pickaxe. > > > The first question regards creating a new instance of the class. The > initialize method expects two integer values. While I have no problem > making sure that they are integers I am not sure what to do if they are not. > Do I return nil or some other result? > > > > > > > You could raise an exception: > > > > unless ... > > raise ArgumentError, "Rational arguments must be integers" > > end > > > > > That worked except for a minor problem. I first tried a.class == Integer > and it failed. I had to use Fixnum to get it to work. I thought that > Integer being the super class for Fixnum and Bignum I could just test for > Integer without having to test for both of the subclasses. Is there any way > to do this? > You could use a.kind_of? Integer, but why not just call a.to_int? to get the value to use instead?