defshortenSMSstr# Get basic message statisticsexcessChars=str.length-160spacesCount=str.count(' ')# How many substitutions should be donesubCount=[excessChars,spacesCount].minsubCount.timesdo# Substitutes with mutation first space from end, capturing the tailstr.sub!(/\s(\S*)$/)do# Upcase first character of tail and append rest# $1.capitalize won't do, because it lowercases rest of the string$1[0].upcase+$1[1..]endend# Ensure that message is 160 chars longstr[0...160]end
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This one ended up being less readable, will give a compact version and expanded version with commentary. Once again, Ruby:
Explained version: