From: Paul Brannan Date: 2002-04-25T06:25:53+09:00 Subject: Re: Numberic#prev On Thu, Apr 25, 2002 at 05:01:32AM +0900, Hal E. Fulton wrote: > Let me ask these questions: > 1. What do we do when we can't figure out a predecessor? > Raise an exception? That would be my first guess. > 2. Are there cases where String#succ is undefined? What > does it do then? > 3. What are the major domains where String#succ is defined? > "A".."Z", "a".."z", "0".."9", and are there more? > 4. Do the rules for concatenation of such strings make things > more difficult? For example: Can you guess the value of > "Z9".succ ? It is "AA0". Therefore "AA0".pred would have to be > "Z9"... These are very good questions. I don't have an answer to any of them. In question #1, by "when we can't figure out" do you mean "when there is more than one predecessor"? Perhaps posting this is not wise, but here is an implementation, with some examples. Maybe this will make the problem more concrete, or maybe it is a Pandora's box. Read it at your own risk. Paul --- class Fixnum def pred return self - 1 end end class String def pred case self[-1] when nil return '' # is this the right thing to return? when ?0 return pred_borrow('9') when ?A return pred_borrow('Z') when ?a return pred_borrow('z') else result = self.dup result[-1] = self[-1].pred return result end end def pred_borrow(end_str) result = self[0..-2] if result.length == 0 then return "" else return result.pred + end_str end end end p ''.pred #=> "" p '9'.pred #=> "8" p '42'.pred #=> "41" p '40'.pred #=> "39" p '10'.pred #=> "09" (should be both '9' and '09') p 'A0'.pred #=> "9" (should be ????) p 'AA0'.pred #=> "Z9" (should be ????) p 'AA'.pred #=> "Z" (should be ????)