Ruby 没有产出
有人能告诉我为什么这个程序不产生输出吗?它应该生成的输出为:行读取:0 行读取:1行读取:2行读取:3,依此类推。 到目前为止,我没有得到一个输出,即使我已经修复了一些bug。任何帮助或建议都将不胜感激Ruby 没有产出,ruby,Ruby,有人能告诉我为什么这个程序不产生输出吗?它应该生成的输出为:行读取:0 行读取:1行读取:2行读取:3,依此类推。 到目前为止,我没有得到一个输出,即使我已经修复了一些bug。任何帮助或建议都将不胜感激 # takes a number and writes that number to a file then on each line # increments from zero to the number passed def write(aFile, number) # You mi
# takes a number and writes that number to a file then on each line
# increments from zero to the number passed
def write(aFile, number)
# You might need to fix this next line:
aFile.puts(number)
index = 0
while (index < number)
aFile.puts(index.to_s)
index += 1
end
end
# Read the data from the file and print out each line
def read(aFile)
# Defensive programming:
count = aFile.gets
if (is_numeric?(count))
count = count.to_i
index = 0
while (index < count)
line = aFile.gets
puts "line read: " + line
index+=1
end
end
end
# Write data to a file then read it in and print it out
def main
aFile = File.new("mydata.txt", "w") # open for writing
write(aFile, 10)
aFile.close
aFile = File.new("mydata.txt", "r")
read(aFile)
aFile.close
end
# returns true if a string contains only digits
def is_numeric?(obj)
if /[^0-9]/.match(obj) == nil
true
end
false
end
main
#获取一个数字并将该数字写入文件,然后写入每一行
#从零递增到传递的数字
def写入(文件、编号)
#您可能需要修复下一行:
aFile.puts(数字)
索引=0
while(索引<编号)
文件放置(索引到)
指数+=1
结束
结束
#从文件中读取数据并打印出每一行
def读取(aFile)
#防御性编程:
count=aFile.get
如果(是数字?(计数))
count=count.to_i
索引=0
while(索引<计数)
line=aFile.get
将“行”改为“+行”
指数+=1
结束
结束
结束
#将数据写入文件,然后读入并打印出来
def总管
aFile=File.new(“mydata.txt”,“w”)#打开进行写入
写入(文件,10)
关闭
aFile=File.new(“mydata.txt”、“r”)
读(文件)
关闭
结束
#如果字符串仅包含数字,则返回true
def是数字的?(obj)
如果/[^0-9]/.match(obj)=nil
真的
结束
假的
结束
主要的
我发现了两个错误。修复这些错误将提供所需的输出
错误#1.
您的方法是数值的?
总是返回false
。即使您的情况为真
。方法的最后一行是false
,因此整个方法始终返回false
。
您可以分两步修复它
第1步:
在条件内返回布尔值不是一个好的做法。您可以通过以下方式进行简化:
def is_numeric?(obj)
/[^0-9]/.match(obj) == nil
end
甚至更好
def is_numeric?(obj)
/[^0-9]/.match(obj).nil?
end
错误#2在您的read
方法中。如果在从文件中读取count
的值后尝试输出该值,它将为您提供“10\n”
。最后那个\n
把你搞砸了
要在读取文件时摆脱\n
,可以使用chomp
。那么你的阅读路线是:
count = aFile.gets.chomp
其余的工作方式就像魔术一样您的代码不是以Ruby方式编写的 如果我想紧密模仿代码的逻辑,我会这样写:
# takes a number and writes that number to a file then on each line
# increments from zero to the number passed
def write_data(fname, counter)
File.open(fname, 'w') do |fo|
fo.puts(counter)
counter.times do |n|
fo.puts n
end
end
end
# returns true if a string contains only digits
def is_numeric?(obj)
obj[/^\d+$/]
end
# Read the data from the file and print out each line
def read_data(fname)
File.open(fname) do |fi|
counter = fi.gets.chomp
if is_numeric?(counter)
counter.to_i.times do |n|
line_in = fi.gets
puts 'Line read: %s' % line_in
end
end
end
end
# Write data to a file then read it in and print it out
DATA_FILE = 'mydata.txt'
write_data(DATA_FILE, 10)
read_data(DATA_FILE)
哪些产出:
Line read: 0
Line read: 1
Line read: 2
Line read: 3
Line read: 4
Line read: 5
Line read: 6
Line read: 7
Line read: 8
Line read: 9
注意这些事情:
- 方法(或变量)名称在Ruby中不是camelCase格式,而是snake_格式。这是一个很好的例子
- Ruby鼓励我们在打开文件进行读写时使用块,在完成文件后自动关闭文件。在一个长时间运行的程序中,让挂起的文件句柄保持打开状态而不是关闭状态,这是一种很好的方法,可以让您的程序以一种难以理解的方式崩溃。这样做会产生很多问题。这来自文档:
没有关联的块,
是::open
的同义词。如果给出了可选代码块,它将作为参数传递::new
,并且当该块终止时,io对象将自动关闭。在本例中,io
返回块的值 通常您会看到代码使用::open
而不是File.open
,这主要是Ruby程序员的习惯。文件继承自IO,并向类中添加了一些额外的面向文件的方法,因此它的功能更加全面IO.open
- Ruby有许多方法可以帮助我们避免使用
循环。获取错误的计数器或缺少终止循环的条件在编程中非常常见,因此Ruby使循环“n次”或迭代数组中的所有元素变得非常容易。该方法很好地实现了这一点while
- String的
方法非常强大,可以轻松查看字符串的内容并应用模式或片段。使用[]
检查整个字符串以确保所有字符都是数字,因此/^\d+$/
的版本比您正在执行的操作要短,并且完成相同的操作,返回一个“truthy”值某些字符串[/^\d+$/]
- 我们不使用
方法。这是老式的Pascal、C或Java,是人工构造的。鲁比比这更友好一点main
3.times do |n|
puts n
end
# >> 0
# >> 1
# >> 2
我可能会用
puts (0..(3 - 1)).to_a * "\n"
# >> 0
# >> 1
# >> 2
只是因为我倾向于用Perl术语思考。这是另一个老习惯。编辑问题,告诉我们你想完成什么,而不仅仅是你在做什么。即使在尝试添加行号或递增记录时,编写和读取文件也比您正在做的要容易得多。给我们展示一个输出和输入文件的小例子。请参阅“”和“”及其所有链接页面。另请参见“”。注意:Ruby是一种区分大小写的语言,大写字母在语法方面具有特定的含义。变量和方法名称应为小写字母。大写字母表示形式为
ClassName
或CONSTANT\u NAME
@iblue95的常量:因为至少执行了第一个文件.put(number)
,我们知道number
必须是10
,所以应该有一个非空文件。执行程序后,您可以通过检查文件mydata.txt
来验证这一点。因此,您可以将调试工作集中在读取文件上。@iblue95:Hint:Test您的是数字吗?几个固定字符串(如'34'
,'xxx'
或`'6p'`)的正确性方法
puts (0..(3 - 1)).to_a * "\n"
# >> 0
# >> 1
# >> 2