Hi,

On Mar 5, 2004, at 1:09 AM, illocutionist wrote:

> # An opposite to .succ!   Or, a predecessor method for String.
>
> # This little thing is for my own use, and entertainment
> # There are those that might recoil from by beginners drivel, but I
> # need a little help...
>
> # I keep getting  'out of range' error no matter what I try
> # when "a".pred!
>
> class String
>
>   def pred!
>     begin
> # get the ascii value of the last element in string and decrement it...
>       self[self.size - 1] = (self[self.size - 1] - 1)
>
>       if self[self.size - 1]  == 96        # 'a' is the last char we 
> want
>           self.chop!                         #  when 'a'.pred! .chop 
> char
>           self[self.size - 1] = 122          #  set new last element 
> to 'z'
>       end
>           return self
>     end while self.size > 0                # hmmmm...
>   end
> end

First of all: You're doing too much work! :D there are a couple things 
you an do that will make the code smaller, and even easier to read.

Take this line:
   self[self.size - 1] = (self[self.size - 1] - 1)

first: when using array notation, there is an easier way to get the 
last element. You can leave off the "self.size" and just have the -1 
part
   self[-1] = (self[-1] - 1)

next, since array notation can be used for both assignment and 
retrieval, you can use "-=" :)
   self[-1] -= 1 # this decrements the last character

However, I think that there is a logic error in your code...
  >> 'cba'.pred!
=> "cz"
  >> 'cba'.pred!.succ!
=> "da"

shouldn't "'cba'.pred!.succ!" return 'cba'?

looking at your code, I see that you are decrementing the last 
character, then chopping it if it is under-valued. What you really want 
to do is decrement the character to it's left, and set the last 
character to 'z'.

   if self[-1] > 97 # it's bigger than a
     self[-1] -= 1
   else # it is a. look to the left.
     # decrement char to left, set self[-1] to z
     # unless there is no char to the left, in which case, return ''
   end

There's more, but I'll leave the rest as an exercise ;)

--Mark