在 Ruby 中读取文件的第一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1490138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading the first line of a file in Ruby
提问by Craig Walker
I want to read onlythe first line of a file using Ruby in the fastest, simplest, most idiomatic way possible. What's the best approach?
我想以最快、最简单、最惯用的方式使用 Ruby仅读取文件的第一行。最好的方法是什么?
(Specifically: I want to read the git commit UUID out of the REVISION file in my latest Capistrano-deployed Rails directory, and then output that to my tag. This will let me see at an http-glance what version is deployed to my server. If there's an entirely different & better way to do this, please let me know.)
(具体来说:我想从我最新的 Capistrano 部署的 Rails 目录中的 REVISION 文件中读取 git commit UUID,然后将其输出到我的标签。这将让我一目了然地看到部署到我的服务器的版本。如果有完全不同的更好的方法来做到这一点,请告诉我。)
回答by Chuck
This will read exactly one line and ensure that the file is properly closed immediately after.
这将准确读取一行并确保文件在之后立即正确关闭。
strVar = File.open('somefile.txt') {|f| f.readline}
# or, in Ruby 1.8.7 and above: #
strVar = File.open('somefile.txt', &:readline)
puts strVar
回答by Blake Taylor
Here's a concise idiomatic way to do it that properly opens the file for reading and closes it afterwards.
这是一种简洁的惯用方法,可以正确打开文件进行阅读并在之后关闭它。
File.open('path.txt', &:gets)
If you want an empty file to cause an exception use this instead.
如果您希望空文件导致异常,请改用它。
File.open('path.txt', &:readline)
Also, here's a quick & dirty implementation of head that would work for your purposes and in many other instances where you want to read a few more lines.
此外,这是 head 的一个快速而肮脏的实现,可以满足您的目的以及在您想要阅读更多行的许多其他情况下。
# Reads a set number of lines from the top.
# Usage: File.head('path.txt')
class File
def self.head(path, n = 1)
open(path) do |f|
lines = []
n.times do
line = f.gets || break
lines << line
end
lines
end
end
end
回答by Vincent
You can try this:
你可以试试这个:
File.foreach('path_to_file').first
回答by jkupferman
How to read the first line in a ruby file:
如何读取 ruby 文件中的第一行:
commit_hash = File.open("filename.txt").first
Alternatively you could just do a git-log from inside your application:
或者,您可以从应用程序内部执行 git-log:
commit_hash = `git log -1 --pretty=format:"%H"`
The %H tells the format to print the full commit hash. There are also modules which allow you to access your local git repo from inside a Rails app in a more ruby-ish manner although I have never used them.
%H 告诉格式打印完整的提交哈希。还有一些模块允许您从 Rails 应用程序内部以更像红宝石的方式访问本地 git 存储库,尽管我从未使用过它们。
回答by Jeremy Ruten
first_line = open("filename").gets
回答by Andy Atkinson
I think the jkupferman suggestion of investigating the git --prettyoptions makes the most sense, however yet another approach would be the headcommand e.g.
我认为 jkupferman 对调查git --pretty选项的建议最有意义,但是另一种方法是head命令,例如
ruby -e 'puts `head -n 1 filename`' #(backtick before `head` and after `filename`)
回答by boblin
first_line = File.readlines('file_path').first.chomp
回答by markeissler
Improving on the answer posted by @Chuck, I think it might be worthwhile to point out that if the file you are reading is empty, an EOFError exception will be thrown. Catch and ignore the exception:
改进@Chuck 发布的答案,我认为值得指出的是,如果您正在阅读的文件为空,则会引发 EOFError 异常。捕获并忽略异常:
def readit(filename)
text = ""
begin
text = File.open(filename, &:readline)
rescue EOFError
end
text
end

