Ruby: Is there something like Enumerable#drop that returns an enumerator instead of an array?

拈花ヽ惹草 提交于 2019-12-04 03:15:48

If you need it more than once, you could write an extension to Enumerator.

class Enumerator
  def enum_drop(n)
    with_index do |val, idx|
      next if n == idx
      yield val
    end
  end
end

File.open(testfile).each_line.enum_drop(1) do |line|
  print line
end

# prints lines #1, #3, #4, …

This is slightly neater:

File.open(fname).each_line.with_index do |line, lineno|
  next if lineno == 0
  # ...
end

or

io = File.open(fname)
# discard the first line
io.gets
# process the rest of the file
io.each_line {|line| ...}
io.close

Now that you've gotten reasonable answers, here's a completely different way to handle it.

class ProcStack
  def initialize(&default)
    @block = default
  end
  def push(&action)
    prev = @block
    @block = lambda do |*args|
      @block = prev
      action[*args]
    end
    self
  end
  def to_proc
    lambda { |*args| @block[*args] }
  end
end
#...
process_lines = ProcStack.new do |line, index|
  puts "processing line #{index} => #{line}"
end.push do |line, index|
  puts "skipping line #{index} => #{line}"
end
File.foreach(filename).each_with_index(&process_lines)

It's neither idiomatic, nor terribly intuitive the first time through, but it's fun!

Off the top of my head but I'm sure with some more research there's a more elegant way

File.open( filename ).each_line.to_a[1..-1].each{ |line|... }

Okay scratch that... did a bit of research and this might be better

File.open( filename ).each_line.with_index.drop_while{ |line,index|  index == 0 }.each{ |line, index| ... }

I doubt that this is idiomatic, but it's simple.

f = File.open(filename)
f.readline
f.each_line do |x|
   #...
end

I think you are right on track with the Enumerator and drop(1). For some odd reason, while Enumerable defines #drop, Enumerator does not. Here is a working Enumerator#drop:

  class Enumerator
    def drop(n_arg)
      n = n_arg.to_i # nil becomes zero
      raise ArgumentError, "n must be positive" unless n > 0
      Enumerator.new do |yielder|
        self.each do |val|
          if n > 0
            n -= 1
          else
            yielder << val
          end
        end
      end
    end
  end
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!