def samecase(padrao, alvo)
alvo.split("").zip(padrao.split(""))
.map { |a, p| p.upcase == p ? a.upcase : a.downcase }
.join("")
end
puts "Testando samecase"
puts samecase("abcd", "XYZW")
puts samecase("aBcD", "XYZW")
puts samecase("aBcD", "xyzw")
puts samecase("ABCD", "xyzw")
def replace_with_case(str, padrao, novo_valor)
str.gsub(/#{padrao}/i) { |matched| samecase(matched, novo_valor) }
end
puts "Testando replace_with_case"
puts replace_with_case("dado", "da", "la")
puts replace_with_case("DaDo", "da", "la")
puts replace_with_case("dAdO", "da", "la")
puts replace_with_case("dado", "DA", "LA")
puts replace_with_case("daDO", "DA", "LA")
ZGVmIHNhbWVjYXNlKHBhZHJhbywgYWx2bykKICAgIGFsdm8uc3BsaXQoIiIpLnppcChwYWRyYW8uc3BsaXQoIiIpKQoJCS5tYXAgeyB8YSwgcHwgcC51cGNhc2UgPT0gcCA/IGEudXBjYXNlIDogYS5kb3duY2FzZSB9CgkJLmpvaW4oIiIpCmVuZApwdXRzICJUZXN0YW5kbyBzYW1lY2FzZSIKcHV0cyBzYW1lY2FzZSgiYWJjZCIsICJYWVpXIikKcHV0cyBzYW1lY2FzZSgiYUJjRCIsICJYWVpXIikKcHV0cyBzYW1lY2FzZSgiYUJjRCIsICJ4eXp3IikKcHV0cyBzYW1lY2FzZSgiQUJDRCIsICJ4eXp3IikKCmRlZiByZXBsYWNlX3dpdGhfY2FzZShzdHIsIHBhZHJhbywgbm92b192YWxvcikgCglzdHIuZ3N1YigvI3twYWRyYW99L2kpIHsgfG1hdGNoZWR8IHNhbWVjYXNlKG1hdGNoZWQsIG5vdm9fdmFsb3IpIH0KZW5kCnB1dHMgIlRlc3RhbmRvIHJlcGxhY2Vfd2l0aF9jYXNlIgpwdXRzIHJlcGxhY2Vfd2l0aF9jYXNlKCJkYWRvIiwgImRhIiwgImxhIikKcHV0cyByZXBsYWNlX3dpdGhfY2FzZSgiRGFEbyIsICJkYSIsICJsYSIpCnB1dHMgcmVwbGFjZV93aXRoX2Nhc2UoImRBZE8iLCAiZGEiLCAibGEiKQpwdXRzIHJlcGxhY2Vfd2l0aF9jYXNlKCJkYWRvIiwgIkRBIiwgIkxBIikKcHV0cyByZXBsYWNlX3dpdGhfY2FzZSgiZGFETyIsICJEQSIsICJMQSIpCg==