小编Kri*_*lsh的帖子

file.each_line只能调用一次

我在迭代文件的行时遇到一些问题,似乎每个文件只能使用一次each_line方法

file = open_file(path)
file.each_line { puts "Q"}
puts "--"
file.each_line { puts "Q"}
puts "--"
file.each_line { puts "Q"}
puts "--"
file.each_line { puts "Q"}

#Output: (on a file with three lines in it )
#Q
#Q
#Q
#--
#--
#--
Run Code Online (Sandbox Code Playgroud)

它与常规迭代器一起工作正常

3.times { puts "Q"}
puts "--"
3.times { puts "Q"}
puts "--"
3.times { puts "Q"}
puts "--"
3.times { puts "Q"}

#Output: (on a file with three lines in it )
#Q
#Q
#Q
#--
#Q …
Run Code Online (Sandbox Code Playgroud)

ruby iteration

3
推荐指数
1
解决办法
378
查看次数

标签 统计

iteration ×1

ruby ×1