input_file = ARGV.first
def print_all(f)
puts f.read
end
def rewind(f)
f.seek(0)
end
def print_a_line(line_count, f)
puts "#{line_count}, #{f.gets.chomp}"
end
current_file = open(input_file)
puts "First let's print the whole file:¥n"
print_all(current_file)
puts "Let's rewind kind a like a tape"
rewind(current_file)
puts "Let's print three lines:"
current_line = 1
print_a_line(current_line, current_file)
current_line += 1
print_a_line(current_line, current_file)
私は確かにこれに似たポストがあると確信していますが、私の質問は少し異なります。上記のように、print_a_lineメソッドには、line_countとfの2つのパラメータがあります。gets.chompを引数として渡す
1)私が理解しているように、line_count引数は、current_lineという変数としてのみ機能し、単なる整数です。私は、コードを実行すると、メソッドprint_a_lineがこれを示すため、それは、巻き戻し(F)方法に関するない方法:
1は、第1ラインであり、2は第二ある1, Hi
2, I'm a noob
。 line_countは数字に過ぎません。rubyは1が1行目で2が2行目であることをどのように知っていますか?
2)なぜprint.a_lineメソッドでgets.chompを使用しますか?私はちょうどこの
def print_a_line(line_count, f)
puts "#{line_count}, #{f}"
end
のようなfを渡すと私はIO#gets
が読めるI/Oストリームから次の行を読み取るため
1, #<File:0x007fccef84c4c0>
2, #<File:0x007fccef84c4c0>