Loop starting not from beginning

Hi sir Nobu,

You suggested awhile back:

And regarding original post,

open(“test”) do |f|
4.times {f.readline}
f.each {|line| p line}
end

I’ve made a function to make the code generic like:

def Fp (file,skip)
File.open(file) do |f|
skip.times {f.readline}
f.each { |line| p line }
^^^^^^^^^^^^^^^^^^
end
end

The each block isn’t generic (note underscored). I want to pass my own block
for each. How do I do that? Maybe my approach is wrong? Sorry, I cannot find
samples…

Nobu Nakada

Best regards,
-botp

Hi,

The each block isn’t generic (note underscored). I want to pass my own block
for each. How do I do that? Maybe my approach is wrong? Sorry, I cannot find
samples…

Do you want to write like as:
Fp(“input.txt”, 4) {|line| p line}
?

def Fp (file,skip)
File.open(file) do |f|
skip.times {f.readline}
f.each { |line| p line }
f.each { |line| yield line }
end
end

Or

def Fp(file, skip, &block)
File.open(file) do |f|
skip.times {f.readline}
f.each(&block)
end
end

···

At Sat, 30 Nov 2002 16:16:01 +0900, Peña, Botp wrote:


Nobu Nakada