确定 Ruby 中的最后一行

发布于 2024-12-11 13:30:47 字数 193 浏览 0 评论 0原文

我想知道如何确定何时位于我读入的文件的最后一行。我的代码看起来像是

File.open(file_name).each do |line|
    if(someway_to_determine_last_line)
end

我注意到有一个 file.eof?方法,但是在读取文件时如何调用该方法?谢谢!

I'm wondering how I can determine when I am on the last line of a file that I reading in. My code looks like

File.open(file_name).each do |line|
    if(someway_to_determine_last_line)
end

I noticed that there is a file.eof? method, but how would I call the method as the file is being read? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

卖梦商人 2024-12-18 13:30:47

如果您使用 each 迭代文件,那么最后一行将在到达文件末尾之后传递到块,因为最后一行是,根据定义,该行以 EOF 结尾。

因此只需在块中调用 file.eof? 即可。

如果您想确定它是否是文件中的最后一个非空行,您必须实现某种预读。

If you're iterating the file with each, then the last line will be passed to the block after the end-of-file is reached, because the last line is, by definition, the line ending with EOF.

So just call file.eof? in the block.

If you'd like to determine if it's the last non-empty line in the file, you'd have to implement some kind of readahead.

月光色 2024-12-18 13:30:47

根据您需要对“最后一个非空行”执行的操作,您可能可以执行以下操作:

last_line = nil
File.open(file_name).each do |line|
    last_line = line if(!line.chomp.empty?)
    # Do all sorts of other things
end
if(last_line)
    # Do things with the last non-empty line.
end

Depending on what you need to do with this "last non-empty line", you might be able to do something like this:

last_line = nil
File.open(file_name).each do |line|
    last_line = line if(!line.chomp.empty?)
    # Do all sorts of other things
end
if(last_line)
    # Do things with the last non-empty line.
end
走走停停 2024-12-18 13:30:47

秘密武器是 .to_a

lines = File.open(filename).to_a

获取第一行:

puts lines.first

获取最后一行:

puts lines.last

获取文件的 n 行:

puts lines.at(5)

获取行数:

puts lines.count

Secret sauce is .to_a

lines = File.open(filename).to_a

Get the first line:

puts lines.first

Get the last line:

puts lines.last

Get the n line of a file:

puts lines.at(5)

Get the count of lines:

puts lines.count
平定天下 2024-12-18 13:30:47

fd.eof? 可以工作,但只是为了好玩,这里有一个适用于任何类型的枚举器的通用解决方案(Ruby 1.9):

class Enumerator
  def +(other)
    Enumerator.new do |yielder|
      each { |e| yielder << e }
      other.each { |e| yielder << e }
    end
  end

  def with_last
    Enumerator.new do |yielder|
      (self + [:some_flag_here]).each_cons(2) do |a, b|
        yielder << [a, b == :some_flag_here]
      end
    end
  end
end

# a.txt is a file containing "1\n2\n3\n"
open("a.txt").lines.with_last.each do |line, is_last|
  p [line, is_last]
end

输出:

["1\n", false]
["2\n", false]
["3\n", true]

fd.eof? works, but just for fun, here's a generic solution that works with any kind of enumerators (Ruby 1.9):

class Enumerator
  def +(other)
    Enumerator.new do |yielder|
      each { |e| yielder << e }
      other.each { |e| yielder << e }
    end
  end

  def with_last
    Enumerator.new do |yielder|
      (self + [:some_flag_here]).each_cons(2) do |a, b|
        yielder << [a, b == :some_flag_here]
      end
    end
  end
end

# a.txt is a file containing "1\n2\n3\n"
open("a.txt").lines.with_last.each do |line, is_last|
  p [line, is_last]
end

Which outputs:

["1\n", false]
["2\n", false]
["3\n", true]
指尖上得阳光 2024-12-18 13:30:47

打开文件并使用 readline 方法:

要简单地操作文件的最后一行,请执行以下操作:

f = File.open('example.txt').readlines
f.each do |readline|
  if readline[f.last]
    puts "LAST LINE, do something to it"
  else
    puts "#{readline} "
  end
end

第 1 行将文件作为行数组读取

第 2 行使用该对象并迭代每个对象

第 3 行测试当前行是否匹配 匹配,则最后一行

如果与

第 5 行和第 5 行 第 4 行将起作用。 6 不匹配情况的处理行为

Open your file and use the readline method:

To simply manipulate last line of file do the following:

f = File.open('example.txt').readlines
f.each do |readline|
  if readline[f.last]
    puts "LAST LINE, do something to it"
  else
    puts "#{readline} "
  end
end

Line 1 reads the file in as an array of lines

Line 2 uses that object and iterates over each of them

Line 3 tests if the current line matches the last line

Line 4 acts if it's a match

Line 5 & 6 handle behavior for non-matching circumstance

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文