Skip to content

Instantly share code, notes, and snippets.

@yugi386
Created December 28, 2011 14:13
Show Gist options
  • Save yugi386/1528065 to your computer and use it in GitHub Desktop.
Save yugi386/1528065 to your computer and use it in GitHub Desktop.
Exercício de Máscara - Referente so Gist https://gist.github.com/1182380
#######################################
# EXERCÍCIO PARA MÁSCARA
# PROGRAMADOR: YUGI
# DATA: 28/03/2011
#######################################
#
# Deve completar a máscara de acordo com o número informado
#
=begin
mask("125")
=> "00012-500"
mask("385002")
=> "38500-200"
mask("38500201")
=> "38500-201" }
mask("2950")
=> "00295-000"
mask("22923")
=> "02292-300"
mask("7603725")
=> "76037-250"
=end
def mask(valor)
tam = valor.length
sep = "-"
retorno = ""
if tam < 3 || tam > 8
puts "Parâmetro Inválido!"
return retorno
elsif tam == 8
retorno = valor[0,5] + sep + valor[5,3]
elsif tam == 7
valor = valor + "0"
retorno = valor[0,5] + sep + valor[5,3]
elsif tam == 6
valor = valor + "00"
retorno = valor[0,5] + sep + valor[5,3]
elsif tam == 5
valor = "0" + valor + "00"
retorno = valor[0,5] + sep + valor[5,3]
elsif tam == 4
valor = "00" + valor + "00"
retorno = valor[0,5] + sep + valor[5,3]
elsif tam == 3
valor = "000" + valor + "00"
retorno = valor[0,5] + sep + valor[5,3]
end
puts retorno
return retorno
end
# =======================================================================================
mask("12")
mask("123456789")
mask("125")
mask("385002")
mask("38500201")
mask("2950")
mask("22923")
mask("7603725")
mask("12345678")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment