From: ts Date: 2003-12-31T23:50:04+09:00 Subject: Re: Overriding to_s >>>>> "B" == Brad writes: B> Is there a way to override the to_s method such that it takes a B> parameter? For example, if I wanted to provide a format specifier B> for a number: well, you have several problems B> class Numeric #to_s is defined in Integer, Float, Bignum, ... not in Numeric If you define it in Numeric, ruby will use the old definition in Integer, ... B> def to_s(optFormat) you are saying that #to_s must have a parameter, this mean that you can't call it 12.to_s Perhaps best to write it def to_s(optFormat = nil) now Integer#to_s can take an optional argument, this mean that your new method #to_s is not really adapted for Integer (except if format_number take this in count) B> if !optFormat.nil? B> # format a string based on the optFormat format B> # specifier. B> retVal = format_number(self,optFormat) B> else B> retVal = self.to_s here, you call recursively #to_s. Perhaps best to alias #to_s before redefining it, and call the alias B> end B> retVal B> end B> end Guy Decoux