remove a substring from a string in Ruby
Code snippet on how to remove a substring from a string in Ruby
def remove_substring(str, substr)
str.gsub(substr, "")
end
This code snippet is written in the Ruby programming language and shown how to remove a substring from a string. The code takes two arguments: a string and a substring. The gsub
method is used to remove the substring from the string, and the empty string "" is used as the replacement for the substring. The gsub
method is part of the String class and will replace any occurrence of the substring with the replacement String given. The final result is a string without the substring.