From: dblack@... Date: 2007-07-22T18:44:55+09:00 Subject: Re: Is there a replacement for sub? Hi -- On Sun, 22 Jul 2007, Michael W. Ryder wrote: > dblack@wobblini.net wrote: >> Hi -- >> >> On Sat, 21 Jul 2007, Michael W. Ryder wrote: >> >>> Sebastian Hungerecker wrote: >>>> Michael W. Ryder wrote: >>>>> Martin DeMello wrote: >>>> >>>>>> a = "a b c d e f" >>>>>> b = a >>>>>> a = a.sub(" ", "") # => a is now "ab c d e f", b is still "a b c d e f" >>>>> Wrong. >>>> >>>> Nope. >>>> >>>>> Unless you use b = a.dup b will be the same as a as it is pointing to >>>>> the same object. >>>> >>>> Yes. >>>> >>>>> The result of the sub operation will change b as well as a. >>>> >>>> No, sub won't change the actual string - that's what sub! would do. It >>>> will >>>> create a new string which will be assigned to a, at which point a and b >>>> won't >>>> be pointing at the same object anymore. >>>> >>>> >>> My mistake. Too many tests with and without the ! operator. I guess that >>> my test used sub! instead of sub. This just proves my point that the >>> whole thing is too confusing. >> >> Keep in mind that the ! means that the method is the "dangerous" >> version. In this case, the danger consists of both the changing of >> the object in place, and the returning of nil when there's no change. >> Any time you see a ! in a method name you've been warned that there's >> probably something you need to be particular aware of and careful >> about. >> >> > Unfortunately, my background has always been that a function never returns > nil if nothing is done. Think about the implications of that, though: what you're saying, in effect, is that Ruby should adapt itself in great detail to everyone's background -- which really means that Ruby should do everything the same way *all* other languages (since someone has a background in every language) do it. Of course, there's no such thing :-) It's not that Ruby is above criticism, of course; but it's best to take it on its own terms as much as possible, and you'll find that things are very well planned and thought through. The ! isn't supposed to be a trap or a surprise; on the contrary, it's an explicit indicator of exactly what you're concerned about -- namely, the fact that the behavior of this method may do "dangerous" things that the the non-! version doesn't do. Have a look at: http://www.infoq.com/articles/coming-from-ruby for more encouragement along these lines :-) > C has the ++ operator which can be very dangerous but it never > returns nil. It also doesn't tell you, right in its name, that you should be cautious about danger. > It may be safer to use a = a + 1 or even better b = a + 1, but I > don't have to worry that a++ will return 0 unless a was -1 before. I > realize that the ! operator is a shortcut, but if it is going to be > a shortcut that is all it should be. It's not a shortcut; as Robert K. said, it's just the last character in the method name. It has no language-level significance, but the contract in place is that it means "dangerous". David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)