Skip to content

Instantly share code, notes, and snippets.

@vlaube
Last active June 15, 2021 16:44
Show Gist options
  • Save vlaube/6ce3914614a06662d27206b3125aa099 to your computer and use it in GitHub Desktop.
Save vlaube/6ce3914614a06662d27206b3125aa099 to your computer and use it in GitHub Desktop.
str = "Vitor Henry Silva Joao Maria Laube"
def abrevia_nome(str, max)
return str if str.size <= max
tmp = str.split(" ")
tmp1 = tmp[1..-2]
tmp2 = tmp[1..-2].map{|a| "#{a[0].upcase}."}
retorno = tmp1
final = ([tmp.first] + retorno + [tmp.last]).join(" ")
posicao = 0
while final.size > max && posicao < tmp2.size
retorno[posicao] = tmp2[posicao]
final = ([tmp.first] + retorno + [tmp.last]).join(" ")
posicao = posicao + 1
end
if final.size > max
posicao = 0
while final.size > max && posicao < tmp2.size
retorno[posicao] = nil
final = ([tmp.first] + retorno.compact + [tmp.last]).join(" ")
posicao = posicao + 1
end
end
return final[0,max]
end
test = *(10..str.length)
test.each do |a|
puts abrevia_nome(str, a)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment