In Ruby we have methods like string.upcase which returns a new string and string.upcase! which modifies the string that called it.
I want to know how one goes about writing their own ! version of a method.
I am writing a method that searches for a string between two chars. I would like it to modify the string to equal the new string.
How does one accomplish this. (I have the method for finding the new string just need to know how to modify the caller)
ANSWER
class String
def find_between(marker_one , marker_two)
self[/#{Regexp.escape(marker_one)}(.*?)# {Regexp.escape(marker_two)}/m, 1]
end
def find_between!(marker_one , marker_two)
self.replace(find_between(marker_one, marker_two))
end
end
In the end self.replace was what was needed