From: "David A. Black" Date: 2008-08-10T19:47:12+09:00 Subject: [ruby-core:18230] Re: New array methods cycle, choice, shuffle (plus bug in cycle) Hi -- On Sun, 10 Aug 2008, gdefty@attglobal.net wrote: > I read and agreed with the blog (except where it > says that not all ! methods modify the receiver - > they do seem to, with the couple of exceptions > noted above). Most of them do, but that's not how Matz has characterized the !. It's more that receiver-changing is a (very common) form of "danger". Also, the blog post is mostly advice about what we should do in our own code, in order not to reinvent and dilute the ! convention. > | And of course it's all part of "danger".[1] > > But this part causes me some concern. As you state > in the blog, it is a subjective (and therefore our > purposes imprecise) term. What we are saying is > that to qualify for a ! a method must satisfy some > criteria which depends on how you are using it. > Not a healthy statement in a programming language > description IMHO. It's quite viable, though. The ! means: "Heads up! This version of the method is different from the one you may have been using!" So, whatever happens, you've been warned that you need to pay particularly close attention to the method's documented behavior. > As an example, if I inadvertently use > > acct = account.fetch(12345) > acct.credit(value) # error - should be > acct.credit!(value) > acct.save > > it would appear that the non-bang version is the > "dangerous" one. But in this case, "credit" already means: credit this account with some money. There's no need for a bang/non-bang pairing. If the non-bang one doesn't credit the account, then it's misnamed. Or you could, as you imply, have the ! one just tell you what the credit would be, without doing it (though it might be better to use "+" for that). Also, there's really no safeguard against inadvertently using the wrong method :-) But in your example, I think you'd be fine with just a "credit" method. It says what it does. > I think the danger here is the use of the word "danger". You're reversing the logic, though, by creating a pair of methods and then trying to determine which one is "dangerous". The vast majority of methods just have a name, and don't have a ! "evil twin". There's no guarantee that there's a dangerous version of every non-! method, so there are many cases where it makes no sense to create the ! one. David -- Rails training from David A. Black and Ruby Power and Light: * Advancing With Rails August 18-21 Edison, NJ * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates!