Hi --

On Tue, 4 Dec 2007, John Joyce wrote:

>
> On Dec 2, 2007, at 1:44 AM, David A. Black wrote:
>
>> Hi --
>> 
>> On Sun, 2 Dec 2007, John Joyce wrote:
>> 
>>> To put it another way, consistency for consistency's sake alone is not the 
>>> goal.
>>> Consistency helps make things somehow predictable, but sometimes it's just 
>>> bloat.
>>> 
>>> That said, if Ruby had immutable classes, it would be important to have
>>> push		# return a new Array object with the result of a push
>>> push!	# alter the Array object in place with the result of push
>>> pop		# return a new Array object with the result of a pop
>>> pop!		# alter the Array object in place with the result of pop
>> 
>> I think the whole question of what the best names were for those
>> methods would have to be revisited. Since push and pop are, by
>> convention and tradition, object-mutating methods, it would be very
>> weird to suddenly have "non-dangerous" versions of them that didn't
>> change the object, and to define "danger" as behaving the way push and
>> pop are always (as far as I know) understood to behave.
>> 
>> 
>> David
> I understand where you're coming from completely with the tradition and 
> convention, but sometimes tradition could be broken.

Yes, of course the parser will let you do:

   def greet!
     puts "Hi"
   end

   def valid?
     @x = 3
   end

but why would you want to? :-) The point is that in this case there is
*only* tradition and convention -- meaning, Matz added these !-methods
to the language *so that* there could be dangerous/non-dangerous
method pairs. Any rededication of ! to another purpose (like, a vague
stylistic flourish meaning "What a strange method!" or a more general
"This changes its receiver" signal) just means that the whole thing
becomes method-name soup and has *no* meaning.

So it's not pure stodginess on my part when I say: let's let the ! do
what it's supposed to do. If we don't, then we've lost what I think is
an ingenious and extraordinarily expressive technique.

> I was just being hypothetical, after playing with Cocoa and Objective-C 
> lately, where most container classes come in mutable and immutable varieties, 
> methods there to work on immutable containers have no choice but to return a 
> new container. (where the new one can be either mutable or immutable, since 
> creation uses the same shared initializer)
> Just to illustrate the point if there were a purpose for ! with push and pop.
> Since we don't have immutable containers in Ruby, there's not a point to 
> these methods having a bang.
> They inherently bang.

They inherently change their receivers, but that's a separate matter
from the !.

> Just a hypothetical.
> Since Ruby is Ruby, the flavor is distinctly mostly mutable objects... yummy!

And, one hopes, mostly non-dangerous operations :-) which is why it's
nice to have the ! to signal those "evil twin" methods.


David

-- 
Upcoming training by David A. Black/Ruby Power and Light, LLC:
    * Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details and 2008 announcements!