在Ruby中使用'+'文件IO模式替换文件中的一行 [英] Replace a line in a file using '+' File IO modes in Ruby

查看:126
本文介绍了在Ruby中使用'+'文件IO模式替换文件中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby的初学者在这里!

我知道Ruby的File.open方法有一些特定的模式,比如r,w,a,r +,w +,a +和互补b 。我完全理解r,w和a模式的用法。但我似乎无法理解如何使用+符号。任何人都可以提供一些链接哪里有例子以及使用它的解释吗?

它可以用来读取一行,并用等量的内容进行编辑/替换吗?如果是这样,那么怎么样?

示例数据文件:a.txt

  aaa 
bbb
ccc
ddd

Demo.rb

  file = File.opena.txt,r +
file.each do | line |
line = line.chomp
if(line ==bbb)then
file.putsbig
end
end
file.close

我试图用big取代bbb,但是我得到这个: - $ b记事本++中的

$ b $ p $ a
bbb

$ b $ ddd
$ / code>

in记事本

  aaa 
bbb
bigddd


解决方案

从另一个答案抢夺了这个文档,所以不是我的,解决方案是我的

  r只读模式。文件指针位于文件的开头。这是默认模式。 
r +读写模式。文件指针将在文件的开头。
w只写模式。如果文件存在,则覆盖该文件。如果文件不存在,则创建一个新的文件进行写入。
w +读写模式。如果文件存在,则覆盖现有的文件。如果文件不存在,则创建一个新的文件进行读写。
a只写模式。如果文件存在,则文件指针位于文件末尾。也就是说,该文件处于附加模式。如果文件不存在,它将创建一个新的文件进行写入。
a +读写模式。如果文件存在,则文件指针位于文件末尾。该文件以附加模式打开。如果文件不存在,它将创建一个新的文件进行读写。

编辑:这里是您的示例的解决方案,大部分时间整个字符串被gsubbed和写回到文件但'infile'替换而不重写整个文件也是可能的
你应该谨慎的用一个相同长度的字符串替换。

<$ p $ file.open('a.txt','r +')do | file |
file.each_line do | line |
if(line =〜/ bbb /)
file.seek(-line.length-3,IO :: SEEK_CUR)
file.write'big'
end
end
end

=>
aaa
big
ccc
ddd

这是一个比较传统的方法,但是大多数其他的解决方案比较简洁。

  File.open(filename =a.txt, r +){| file |文件<< File.read(filename).gsub(/ bbb /,big)} 

EDIT2:i现在认识到这个可以缩短

pre $ File $(File =a.txt,File.read(f).gsub (/ bbb /,big))


Ruby beginner here!

I am aware that Ruby's File.open method has certain modes like r,w,a,r+,w+,a+ and the complimentary b. I totally understand the use of r,w and a modes. But I cannot seem to understand how to use the ones with the '+' symbol. Can anyone provide me with some links where there are examples as well as explanations for the use of it?

Can it be used to read a line and edit/replace it in place by a equal amount of content? If so, then how?

Sample data file: a.txt

aaa
bbb
ccc
ddd

Demo.rb

file = File.open "a.txt","r+"
file.each do |line|
  line = line.chomp
  if(line=="bbb")then
  file.puts "big"
  end
end
file.close

I am trying to replace "bbb" with "big" but I am getting this:- in notepad++

aaa
bbb
big

ddd

in notepad

aaa
bbb
bigddd

解决方案

snatched this documentation from another answer, so not mine, the solution is mine

r  Read-only mode. The file pointer is placed at the beginning of the file. This is the default mode. 
r+ Read-write mode. The file pointer will be at the beginning of the file. 
w  Write-only mode. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. 
w+ Read-write mode. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
a  Write-only mode. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. 
a+ Read and write mode. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

EDIT: here the solution to your sample, most of the time the whole string is gsubbed and written back to the file but 'infile' replacing without rewriting the whole file is also possible You should be cautious to replace with a string of the same length.

File.open('a.txt', 'r+') do |file|
  file.each_line do |line|
    if (line=~/bbb/)
      file.seek(-line.length-3, IO::SEEK_CUR)
      file.write 'big'
    end
  end
end 

=>
aaa
big
ccc
ddd

And this is a more conventional way, though more concise then most other solutions

File.open(filename = "a.txt", "r+") { |file| file << File.read(filename).gsub(/bbb/,"big") } 

EDIT2: i now realize this can still shorter

File.write(f = "a.txt", File.read(f).gsub(/bbb/,"big"))

这篇关于在Ruby中使用'+'文件IO模式替换文件中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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