From: Sylvester Keil Date: 2011-11-14T23:45:25+09:00 Subject: Re: Exclamation marks in method names On Nov 14, 2011, at 2:59 PM, Dave Aronson wrote: > On Mon, Nov 14, 2011 at 07:08, Sylvester Keil wrote: > >> Because I wanted the predicate-setters to be chainable, I made them return self >> always. For instance, I had a Name class that printed names according to >> different formatting rules and I wanted to write things like: >> >> name.sort_order!.to_s #-> set name to sort order formatting and convert to string > > The general idea may be OK, but at least this particular case is a bit > jarring. Reading it, it looks like it sets the sort order to some > default (as opposed to name.sort_order!(Name::Sort_Lexical).to_s), and > converts *that sort order* to a string. IIUC what you're saying, it > would set the sort order, and convert the *name* to a string. The ! > is a bit easy to miss. Yes, the idea was that the !-method returns self, i.e., the name instance. (I realize now that this was a misleading example, because 'sort_order' was a clearly defined predicate in the original context and not akin to Ruby's #sort_by. Sorry for the confusion!) > I think what's irking me may be that the particular method name looks > (if you miss the !) like it's referring to *extracting* state rather > than *setting* it. name.set_sort_order!.to_s or > name.with_sort_order!.to_s might set more correct expectations -- at > least for me, as YM is almost guaranteed to V with something this > subjective. Originally, my motivation was to have a symmetric setter for the ?-methods. Thus, something like: parser.debug? #-> is debug flag true? parser.debug! #-> set debug flag to true That is to say, the !-method will always set the predicate to the state so that the corresponding ?-method will return true.