Christopher Dicely wrote: > 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? > That will not work if the value is a string or some other object that does not implement to_int. If a = "2" entering a.to_int? gives a NoMethodError. I was trying to find a method that would allow any Integer value but raise an exception for others. I will add a coerce method which may be able to handle input from strings or floats at least some of the time. Thanks for the idea for an improvement to my exercise.