From: Paul Brannan Date: 2001-07-13T01:09:35+09:00 Subject: [ruby-talk:17736] Re: Array#sort! returns nil when array empty On Fri, 13 Jul 2001 hfulton@pop-server.austin.rr.com wrote: > And I personally never use this feature -- i.e., I never check > to see whether the receiver was really modified. I just assume > that my postcondition holds. For example, if I use gsub! to > remove commas from a string, I don't care that it didn't have > any commas to begin with. I just know that after the gsub!, it > definitely has no commas. I almost agree. I don't like this feature, but I have used it. In an irc bot, I have the following function: def parse_args(str) arr = Array.new while str[0] != ?: if !(foo = str.slice!(/[^\s]+\s+/)) then # <-- take note arr.push(str) return arr end foo.strip! arr.push(foo) end str.slice!(1..-1) arr.push(str) return arr end This function takes a string of the form "a b c :d e f" and converts it to an array ["a", "b", "c", "d e f"]. Perhaps there is a better way to do this (I'm open to suggestions), but using the return value of slice! seemed like a really easy way to tell if there were no more whole words (i.e. someone left out the ":d e f"). On the other hand, the worst part about the bang functions working this way is that there is no good way to simulate the effect of using gsub vs. gsub!, i.e.: foo = "This is a test" foo.gsub!('foo','bar').gsub!(' ',"\t") does not work, but (foo.gsub!('foo','bar')||foo).gsub!(' ',"\t") does work, but is REALLY ugly. Perhaps, though chaining method calls together like this isn't really a good idea to begin with, and should be discouraged (since we are modifying the original string, the two method calls really are separate operations, and so should probably be on separate lines). Paul