From: Robert Klemme Date: 2007-10-26T17:17:08+09:00 Subject: Re: Skip the first invocation e.g. skip_first { foo } 2007/10/26, Brian Adkins : > On Oct 25, 6:50 pm, Gary Wright wrote: > > On Oct 25, 2007, at 6:40 PM, Brian Adkins wrote: > > > > > Consider the following code: > > > > > first = true > > > 3.times do > > > if first > > > first = false > > > else > > > puts 'foo' > > > end > > > ... > > > end > > > > 3.times do |i| > > print '2nd or more: ' unless i.zero? > > puts "iteration: #{i}" > > end > > I guess I wasn't clear enough. This should work in any type of > iteration, so a loop index may not be available. For example: > > foo.each do |bar| > skip_first { ... } > ... > end Of course there is also a solution with #inject. It can be abused when using this form: foo.inject do |junk,x| puts x end :-) Other than that you could do module Enumerable def skip(n,&b) raise ArgumentError, "invalid skip count" if n<0 each_with_index {|a,i| b[a] if i>=n} end end This could be extended to skip the last n elements if n is negative but this is more complex. Kind regards robert