From: Brian Candler Date: 2003-05-31T06:37:28+09:00 Subject: Re: Iteration - last detection On Sat, May 31, 2003 at 03:13:16AM +0900, Anders Borch wrote: > John Johnson wrote: > >I may be over simplifiying, but Enumerable defines max, so couldn't you: > > > >a = {} > >a["a"] = "A" > >a["b"] = "B" > >a["c"] = "C" > >a.each { |key, val| > > print "#{key} = #{val}" > > if key === a.keys.max > > puts " and that's the last one" > > else > > puts > > end > >} > > that would only work if the array is sorted... More precisely, it only works if the array (or other collection) elements have a defined <=> method which can compare them. irb(main):010:0> [1,5,3,2,1].max => 5 However that's still no good, as it involves iterating over the collection a second time. For something like an IO object, attached to a pipe which can't be rewound, you can't iterative over it twice. Regards, Brian.