krunal shah
krunal shah

Reputation: 16339

Replace string with array content in ruby?

String = "Test string Test"
array = ["link1","link2"]

How can replace string like this ?

Output should be String = "link1 string link2"

Upvotes: 3

Views: 1685

Answers (1)

James Chen
James Chen

Reputation: 10874

String#gsub! could return an enumerator, so this is easy:

string.gsub!("Test").each_with_index { |v, i| array[i] }

Upvotes: 10

Related Questions