ruby - 将 ruby​​ 循环到文本文件

标签 ruby file loops text

我正在尝试制作一个包含大量数字的文本文件。 我现在拥有的代码很好,但我想创建一个循环,用户将能够在输入第一个数字后添加多个数字。 这是我到目前为止的代码。

outFile = File.new("Demo.txt", "a+")
  puts "Please submit the number"
  A = gets
outFile.puts A
outFile.close

最佳答案

这将确保您不会陷入无限循环,因此您可以通过 exitExitquitQuit 停止 并且仍然具有您的功能

outFile = File.new("Demo.txt", "a+")
puts "Please submit the number"
while true
  input = gets.chomp
  break if input.match(/^(ex|qu)(?:it)$/i)
  outFile.puts input
end
outFile.close

关于ruby - 将 ruby​​ 循环到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21463838/

相关文章:

ruby - gem 1.8.10 错误

mysql - 在 Rails 中将两个表与迁移相关

Ruby 字符串处理

Java FileOutputStream 如果不存在则创建文件

sql-server - 使用 SQL Management Studio 将文件上传到 varbinary

Ruby Net::SSH 在 PTY 中获取退出状态

java - 以原子方式将 byte[] 写入文件

java - 如何标量乘数组

loops - Tasm - assembly 循环

c - while 循环中的增量不起作用?