From: Eero Saynatkari Date: 2005-11-09T05:09:18+09:00 Subject: Re: gsub and gsub! are inconsistent aurelianito wrote: > Hi all! > > I've been trying to optimize the code > a_string.gsub(/pattern_1/, "REPLACE_1").gsub(/pattern_2/, > "REPLACE_2").gsub(/PATTERN_3/,"REPLACE_3"). > > with a_string.gsub(/pattern_1/, "REPLACE_1").gsub!(/pattern_2/, > "REPLACE_2").gsub!(/pattern_3/,"REPLACE_3") (note the '!' on the second > and third gsub). > > IMHO, this two blocks of pseudocode should behave in the same way, but > if the second pattern don't matches, it returns null on the second > version, and then generates an exception. > Why is it than the destructive gsub behaves differently? > Do you think that the current behaviour is the right behaviour? Why? There have been many discussions about method chaining. Some think it would be good for bang-methods to always return self, some think that nil should silently consume those method calls and the rest think that you should not be chaining methods in the first place because of all the problems it would mask and think of the children! Yeah, it would be sensible for it to return self. > Please comment, > Aureliano. E