jmichel / schur.institut.math.jussieu.fr (Jean Michel) writes: > Even so I am still surprised. How one is to know that in > > [1].sort! > > the receiver is not changed, but in > > [1,2].sort! > > it is? Not obvious to me... Me neither. Now that I've thought about your example, I don't understand it either. Perhaps someone else can clarify this situation. (I have a feeling it is explained in the pixaxe book, but that book is at home, not here at work.) > >[each_with_index] is defined in the Enumeration (or Enumerable) module. > > > I guess I never tried to look there because for me Enumerable was for > classes were just each exists, no ordering (e.g. is each supposed to > go over the same object in the same order each time?). And why is > the name not each_pair? Hash#each_pair returns Hash key/value pairs. It's really a synonym for Hash#each. Enumerable#each_with_index, since it is mixed in, is available to all classes that implement each and mixin Enumerable. I don't know if each, as defined by classes that mixin Enumerable, should always return values in the same order. I have never relied on that behavior. > >That's certainly possible, but we tend to use blocks instead of anonymous > >subroutines. > > Could you give an example? To give an example of what I was looking for, > I am in the middle of some method with local variables a,b,c and > I have the same piece of code appearing twice: > > some computations > > print {some complicated debug print_out of the current contents of a,b,c} > > some computations modifying a,b,c > > print {same as above} > > Now a,b,c are different classes, and the debug print_out is not associated > especially to any object so in an ordinary programming language (like Pascal) > the best solution would be a local procedure. Is there a better solution > in ruby than what I did: > > debug_print=Proc.new{ the print statement} > > and then do > debug_print.call In this case, I would probably define a "regular" method and call that method twice. Here, I don't see the advantage of using an anonymous subroutine. I'm relatively new to Ruby, and perhaps I am not thinking the "Ruby way". Could someone else please comment on the appropriate use of blocks and/or Proc objects here? Jim -- Jim Menard, jimm / io.com, http://www.io.com/~jimm/ "Java: the elegant simplicity of C++ and the blazing speed of Smalltalk." -- Roland Turner