Mega Code Archive

 
Categories / Ruby / String
 

Get acronym

class String   def acronym(thresh=0)     acro=""     str=self.dup.strip     while !str.nil? && !str.empty?       word = str.shift_word       if word.length >= thresh         acro += word.strip[0,1].to_s.upcase       end     end     acro   end   @@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 s1 = "this is a test" puts s1.acronym