On 29.11.2011 02:05, Josh Cheek wrote: > 2011/11/28 Matthias Wächter<matthias / waechter.wiz.at> > >> A similar solution, at first glance only marginally different, is the >> following: >> >> $stdin.gets >>> puts $stdin.each_line.sort_by(&:to_**i) >>> >> >> Besides the fact that this solution is slower than yours, it uses a neat >> little technique of Ruby 1.9, Enumerators. > > > $stdin is already enumerable, so each_line isn't necessary > > puts $stdin.drop(1).sort_by(&:to_i) Great one-liner, thanks! But in the context of my posting, it is not following the Enumerator principle. $stdin.drop(1) creates a large array on its output which I wanted to avoid. What we need is an enumeratoresque version of Enumerable#drop, which I call drop_first. It will only consume on its input > class Enumerator > def filter1(&blk) > self.class.new do |y| > each do |*input| > y << blk.call(*input) > end > end > end > > def drop_first(n) > self.class.new do |y| > with_index do |input,idx| > y << input if idx >= n > end > end > end > end > > puts $stdin.to_enum(:each_line).drop_first(1).filter1(&:to_i).sort Performance-wise not better at all, but a nice excursion. Matthias