用同一个脚本编写后读取文件(Ruby的硬方法ex16)

用同一个脚本编写后读取文件(Ruby的硬方法ex16),ruby,Ruby,这是我的密码: filename = ARGV.first puts "We're gong to erase #{filename}" puts "If you don't want that, hit CTRL-C (^C)." puts "If you do want that, hit RETURN." $stdin.gets puts "Opening the file..." target = open(filename, 'w') puts "Truncating the

这是我的密码:

filename = ARGV.first

puts "We're gong to erase #{filename}"
puts "If you don't want that, hit CTRL-C (^C)."
puts "If you do want that, hit RETURN."

$stdin.gets

puts "Opening the file..."
target = open(filename, 'w')

puts "Truncating the file. Goodbye!"
target.truncate(0)

puts "Now I'm going to ask you for three lines."

print "line 1: "
line1 = $stdin.gets.chomp
print "line 2: "
line2 = $stdin.gets.chomp
print "line 3: "
line3 = $stdin.gets.chomp

puts "I'm going to write these to the file."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")


print target.read
puts "And finally, we close it."


target.close
我试着让它先写然后读。如果我执行
target.close
,然后在脚本的底部再次执行
target=open(filename)
,它就会工作。还有别的办法吗

我看到了一篇关于python的帖子,其中解释了在写入一个文件后需要关闭它。这同样适用于Ruby吗?我需要用同花顺吗


阅读并关闭后,我是否也应该使用括号?这个例子没有。有两种方法可以解决这个问题。您可以像前面所做的那样,打开文件进行写入、写入、关闭文件,然后重新打开文件进行读取。这很好。关闭文件会将其刷新到磁盘,重新打开文件会将您放回文件的开头

或者,您可以打开一个文件进行读写,并在文件中手动移动,就像编辑器中的光标一样。中定义了执行此操作的选项

代码的问题是这样的

target.write("\n")

print target.read
此时,您正在向该文件写入数据。
目标
文件指针指向文件的末尾,就像编辑器中的光标一样。当您
target.read
时,它将读取文件的结尾,因此您将一无所获。你必须先回到文件的开头

您还必须打开文件进行读写
w+
可以这样做,并为您截断文件

puts "Opening the file..."
target = File.open(filename, 'w+')
这是一种高级技术,最常用于在整个读写过程中锁定文件,以确保没有其他人可以在您读写文件时处理该文件。一般来说,你在阅读和写作时都会这样做。例如,如果您想读取一个文件中的计数器,然后递增,并确保没有人可以在其间写入

def read_and_update_counter
  value = 0

  # Open for reading and writing, create the file if it doesn't exist
  File.open("counter", File::RDWR|File::CREAT, 0644) {|f|
    # Get an exclusive lock to prevent anyone else from using the
    # file while we're updating it (as long as they also try to flock)
    f.flock(File::LOCK_EX)

    # read the value
    value = f.read.to_i

    # go back to the beginning of the file
    f.rewind

    # Increment and write the new value
    f.write("#{value + 1}\n")

    # Flush the changes to the file out of the in-memory IO cache
    # and to disk.
    f.flush

    # Get rid of any other garbage that might be at the end of the file
    f.truncate(f.pos)
  }
  # File.open automatically closes the file for us    

  return value
end

3.times { puts read_and_update_counter }

这不是语言问题,而是文件系统/操作系统问题。你使用的是什么操作系统?Windows具有强制锁定功能,这意味着如果您打开了一个文件,则在该文件关闭之前无法再次打开它。Unix(包括Mac)将允许您在文件上随意涂鸦。这段“Ruby the hard way”代码看起来一点也不像Ruby,它看起来像是Python的逐行直译。“hard way Learn Ruby”似乎是对的直接翻译@steenslag是对的,它不是很好的Ruby。我会避免的。我建议使用“学习Python”来学习编程技术(文件操作技术是通用的),并使用其他类似Ruby的方法来学习。我不推荐。哦,天哪!!你是说我白白浪费了这么多时间?那么除了鹤嘴锄还有什么好呢?在进入railsOP之前,我试图学习大量Ruby,但没有编写代码,这是一个教程(在我看来不是很好的教程)。你喜欢什么教程?@user4584963 the。避免编写Ruby 2.0,这是一本完全不同的书。
def read_and_update_counter
  value = 0

  # Open for reading and writing, create the file if it doesn't exist
  File.open("counter", File::RDWR|File::CREAT, 0644) {|f|
    # Get an exclusive lock to prevent anyone else from using the
    # file while we're updating it (as long as they also try to flock)
    f.flock(File::LOCK_EX)

    # read the value
    value = f.read.to_i

    # go back to the beginning of the file
    f.rewind

    # Increment and write the new value
    f.write("#{value + 1}\n")

    # Flush the changes to the file out of the in-memory IO cache
    # and to disk.
    f.flush

    # Get rid of any other garbage that might be at the end of the file
    f.truncate(f.pos)
  }
  # File.open automatically closes the file for us    

  return value
end

3.times { puts read_and_update_counter }