Hi,

> # 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

IRB can be a big help in trying out bits of code interactively...

irb --simple-prompt

>> x = "a"
=> "a"

# let's try the first line there...
# self[self.size - 1] = (self[self.size - 1] - 1)
>> x[x.size - 1] = x[x.size - 1] - 1
=> 96
>> x
=> "`"
# so far so good...

# ...Incidentally, that line can be written shorter,
# using a negative index to index relative to the end
# of the container, and -= method to decrement...
>> x = "a"
=> "a"
>> x[-1] -= 1    # same as: x[x.size - 1] = x[x.size - 1] - 1
=> 96
>> x
=> "`"
# same result...

# OK.. let's try the next line,
# if self[self.size - 1]  == 96
>> x[-1] == 96   # same thing, using simpler index
=> true

# OK continuing on...
>> x.chop!
=> ""
>> x
=> ""
# well we've chopped our lone character down to nothing...

# we're now about to try this line:
# self[self.size - 1] = 122

# Let's see what self.size - 1 would be...
>> x.size - 1
=> -1

# Hmm, so . . . 
>> x[x.size - 1] = 122
IndexError: index -1 out of string
        from (irb):13:in `[]='
        from (irb):13



So I guess the routine needs some logic to bail out on
a lone 'a' ... Or bail out when the chop! reduces the
string to zero length?  (Not sure what the desired 
outcome is .. :)


Hope this helps,

Regards,

Bill