How to generate string_3 from the difference between string_1 and string_2?

advertisements

I have 2 strings:

string_1 = "abcdefg"

string_2 = "adg"

How to generate string_3 from the difference between string_1 and string_2?

string_3 = "bcef"


String#delete is provided for precisely this purpose. I'm surprised it was suggested earlier. Perhaps some Rubiests think this method is for deleting substrings, which is not correct.

string_1 = "abacdefg"
string_2 = "adg"

string_1.delete(string_2)
  #=> "bcef"

Note that I added an "a" to string_1 to show that both "a"'s are deleted.