fork download
  1. def samecase(padrao, alvo)
  2. alvo.split("").zip(padrao.split(""))
  3. .map { |a, p| p.upcase == p ? a.upcase : a.downcase }
  4. .join("")
  5. end
  6. puts "Testando samecase"
  7. puts samecase("abcd", "XYZW")
  8. puts samecase("aBcD", "XYZW")
  9. puts samecase("aBcD", "xyzw")
  10. puts samecase("ABCD", "xyzw")
  11.  
  12. def replace_with_case(str, padrao, novo_valor)
  13. str.gsub(/#{padrao}/i) { |matched| samecase(matched, novo_valor) }
  14. end
  15. puts "Testando replace_with_case"
  16. puts replace_with_case("dado", "da", "la")
  17. puts replace_with_case("DaDo", "da", "la")
  18. puts replace_with_case("dAdO", "da", "la")
  19. puts replace_with_case("dado", "DA", "LA")
  20. puts replace_with_case("daDO", "DA", "LA")
  21.  
Success #stdin #stdout 0.06s 9792KB
stdin
Standard input is empty
stdout
Testando samecase
xyzw
xYzW
xYzW
XYZW
Testando replace_with_case
lado
LaDo
lAdO
lado
laDO