Mega Code Archive

 
Categories / Ruby / String
 

Rotate right

class String   def rotate_right(n=1)     n=1 unless n.kind_of? Integer     n.times do       char = self.pop       self.unshift(char)     end     self   end   def pop     return nil if self.empty?     item=self[-1]     self.chop!     return nil if item.nil?     item.chr   end   def unshift(other)     newself = other.to_s.dup.pop.to_s + self     self.replace(newself)   end   end a = "this is a test" puts a.rotate_right