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

How can replace string like this ?

Output should be String = "link1 string link2"

1
  • 2
    String is a Ruby standard class name, and local variable should be in lowercase. Commented Aug 11, 2011 at 7:56

1 Answer 1

10

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

string.gsub!("Test").each_with_index { |v, i| array[i] }
Sign up to request clarification or add additional context in comments.

2 Comments

you had written it with gsub earlier, why did u replace it with gsub!? It is always good to avoid gsub! as it modifies the original object.
I did that because from the question I thought krunal wanted the original string to be modified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.