问题描述
初学者学习Ruby。正在做关于读写文件的练习。
name_number = {
}
File.open("book.txt",'a') do |file|
name_number.each do |name,number|
file.write ("- #{name} #{number}\n")
end
file.close
end
File.open("book.txt","r") do |file|
if file.size < 1
puts "There are no people in the book."
end
File.open("book.txt","r") do |file|
puts file.read
end
end
因此它输出“书中没有人”。当文件为空时。
所以,如果我们增加一些人...
name_number = {
"Bill" => 87987,"Kevin" => 78912
}
File.open("book.txt",number|
file.write ("- #{name} #{number}\n")
end
file.close
end
我试图弄清楚怎么说
“有一个人”代表1 和“有人”超过1的数字。
我想我需要一个elsif
这样的语句(但显然这没有用,但是也许您可以看到我在想达到的目标?)。
File.open("book.txt","r") do |file|
if file.size < 1
puts "There are no people in the book."
elsif file.size == 1
puts "There is an entry in the book."
elsif file.size == 2
puts "There are two entries in the book"
end
File.open("book.txt","r") do |file|
puts file.read
end
end
我肯定错过了一些东西。我想知道是否有人可以指出我正确的方向?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)