用Ruby读取前一行文件 [英] Reading previous line of file with Ruby

查看:116
本文介绍了用Ruby读取前一行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何阅读文件的主要行。与IO.gets.I相反,我最初认为将IO.lineno设置为我想要读取的行号,但这并不像预期的那样工作。你如何阅读上一行?

How to read the prevous line of a file. The opposite of IO.gets.I initially thought to set IO.lineno to the line number I wanted to read but that doesn't work as expect. How do you actually read the previous line?

推荐答案

一种简单的方法是记住你读过的上一行:

One simple way is to remember the previous line you read:

prev = nil
File.foreach('_vimrc') do |line|
  p [line, prev]  # do whatever processing here
  prev = line
end

这篇关于用Ruby读取前一行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆