Mega Code Archive

 
Categories / Ruby / String
 

Shifts first word off of self and returns; changes self

class String    @@first_word_re = /^(\w+\W*)/   def shift_word     return nil if self.empty?     self=~@@first_word_re     newself= $' || ""       # $' is POSTMATCH     self.replace(newself) unless $'.nil?     $1   end end  a = "this is a test" puts a.shift_word