Hieu Le
Hieu Le

Reputation: 2136

Ruby: How to replace the words in string by two arrays?

My case:

text = "[A] and [B]"
search = ["[A]", "[B]", "[C]", "[D]", "[Aa]"]
replace = ["[B]", "[C]", "[D]", "[E]", "[Bb]"]

i want to replace value of text to "[B] and [C]"

How can i do that? Thanks a lot!

Upvotes: 3

Views: 90

Answers (1)

Santhosh
Santhosh

Reputation: 29124

text.gsub(/\[\w+\]/, Hash[search.zip replace])
# => "[B] and [C]"

Upvotes: 8

Related Questions