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